Quick start with Postgres ⚡
Tags: postgres, rdms, database, sql
Published: 2022-01-31 03:16:17

---
title: Quick start with Postgres ⚡
published: true
description: Lets learn how to Install, create & insert in PSQL
tags: postgresql, rdms, databases, sql  
cover_image: https://images.unsplash.com/photo-1569235186275-626cb53b83ce?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1472&q=80
---
<h2>0. Introduction</h2>
<p> RDMS are with us for the long time & provides as an organized way to store and access information. In relational
    databases realm, PostgreSQL and MySQL has many similarities.
    <br />
    I am not going to bore you with the specific differences between these two, but if you are curious to read about
    this, I am mentioning 2 articles below here, that clarifies both pro and con about postgres.
    <br />
    1. <a href="https://dzone.com/articles/five-key-postgres-advantages-over-mysql" target="_blank">Dzone's article</a>
    <br />
    2. <a href="https://eng.uber.com/postgres-to-mysql-migration" target="_blank">Uber's article</a>
</p>
<br />

<h2>1. Installation</h2>
<br />
<p>
    I will discuss moslty about installation in linux, but I will also mention links for windows and mac installation.
    <br />
<br />
    Installation steps:
    1. update the current system:
   <br />
    <code> sudo apt update </code>
    <br />
    2. Install the postgresql & contrib package for additions tools.
    <br />
    <code> sudo apt install postgresql postgres-contrib </code>
    <br />
<br />
    great !! installtation is done, now you have postgres in your system.
    <br />
</p>

<br />
<h2>2. Create</h2>
<p>
    PostgresSQL comes with by default user and db named as <b>postgres</b>.
    so let's create a user of your choice first.
    <br />
    <code>
sudo -u postgres createuser  --login --pwprompt test_user
</code>
    <br />
    It will show a prompt for password like below:
<br />
    <code>
Enter password for new role: 
Enter it again: 
</code>

    <br />
<br />
    Lets create a database now:
    <br />
    <code>
sudo -u postgres createdb --owner=test_user test_db
</code>
    <br />
<br />
    now this new "test_db" belongs to the "test_user" and it will be only accessed by test_user.
    <br />
    Now let's restart the postgres service:
<br />
    <code>sudo service postgresql restart</code>
    <br />
<br />
    Now if you're not currently using user profile in which you want to use postgres, then either switch to the that
    user profile example: "postgres" or "test_user" or use psql as that user without switching.
    <br />
<br />
    option 1: switching to different user profile:
    <code> sudo -i -u postgres </code>
    <br />
    then
    <br />
    <code> psql </code>
    <br />
<br />
    option 2: or just use that user to open psql prompt:
    <br />
    <code> sudo -u postgres psql </code>
    <br />
<br />
    Great going !! now lets create our first DB.
    <br />
    <code> sudo create -u postgres createdb test_db </code>
    <br />
    or if you want to create db for another user, you can also do it.
    <br />
    <code> sudo -u postgres createdb --owner=test_user test_db2 </code>
    <br />
    So far we have created a "user" & a "database" in postgreSQL.
    <br />
<br />
    Now understand how to create a table.
    <br />
    <code>
CREATE TABLE table_name (
    column_name1 type (length of field) constraints,
    column_name2 type (length of field),
    column_name3 type (length of field)
);
</code>
    <br />
<br />
    how to read above code ? here :
    <br />
    CREATE TABLE table_name (); is the standard way to initialize a new table. inside that we have columns.
    <br />
    <br />
    1. column_name : name of the column (ex: name, date, username, password, etc.)
    <br />
    2. type: type of column (ex: Interger, varchar,boolean, etc.) with field length (ex: varchar(250) ).
    <br />
    3. constraints: conditions for the column (ex: NOT NULL, check (col_name in ("4 wheeler", "3 wheeler", "16
    wheeler")), UNIQUE, etc.)
    <br />
    <br />
    Let's create a table now:
    <br />
    <code>
CREATE TABLE flights (
    flight_id serial PRIMARY KEY,
    type varchar (50) NOT NULL,
    size varchar (25) NOT NULL,
    location varchar(25) check (location in ('delhi', 'jaipur', 'kolkata', 'gujarat', 'chennai', 'bengalore', 'chandhigarh', 'meghalaya')),
    flight_date date,
    flight_time timestamp
);

</code>
    <br />
    Above command will create a table which you can check by using "\d".
    <br />
</p>
<h2>2. Insert</h2>
<p>
    Now we have created user, database & table in postgreSQL, let's insert a entry.
    <br />
    <code>
INSERT INTO flights(type, size, location, flight_date, flight_time) VALUES('one-way', 'charter', 'delhi', '01-02-2022', '2015-08-07 05:00:01');
</code>
    <br />
    to check if the entered data is saved or not write :
    <br />
    <code>
SELECT * FROM flights;
</code>
    <br />
    And it will show the "flights" table with data in your terminal.
    <br />
    <br />
    <br />
    Awesome 🎊!! we have just learnt basics of postgres and also created & Inserted data in our postgres database. to
    learn further about more postgres topics in deep go to <a
        href="https://www.postgresqltutorial.com/">https://www.postgresqltutorial.com/</a> .
    <br />
    <br />
    Thanks for reading this, Let me know if find any improvisation in this article, I am Prakash Pawar and you can follow me on <a href="https://twitter.com/Thevenicelive"
        target="_blank">twitter</a> & <a href="https://instagram.com/thevenicelive" target="_blank">Instagram</a> . Thank you.
    <br />
</p>