How to setup PostgreSQL in Ubuntu Linux

How to setup PostgreSQL in Ubuntu Linux

PostgreSQL is a Relational Database Management System that provides an implementation of SQL query language support. PostgreSQL is one of the most widely used database management system. It's popularity is based on the stunning features this database provides for creating big scale applications.

In this post, we are gonna be looking at how you can install PostgreSQL in your Ubuntu, Linux.

Let's get started ๐Ÿš€

Step 1: Update The Local Repositories

$ sudo apt-get update

It will update all of your local repositories, a well known step before installing any software in Linux ๐Ÿ˜€

Step 2: Installing PostgreSQL

$ sudo apt-get install postgresql postgresql-contrib

In this step, we are installing two packages postgresql and postgresql-contrib. The first one is the package which will install postgresql in your Linux and the second package i.e postgresql-contrib is responsible for adding some more utilities and functionalities.

Step 3: Confirm Installation

After installing postgresql, we have to confirm whether it's properly installed or not. For this, we can see the config files whether they are available or not. If you have properly installed the database then the config files will be located in the /etc/postgresql/[version_number_here]/main.

Therefore, navigate to this directory by running

$ ls /etc/postgresql/[version_number_here]/main

Replace the version_number_here with the version that you have installed.

The output will look something like this:

$ ls /etc/postgresql/12/main 
conf.d  environment  pg_ctl.conf  pg_hba.conf  pg_ident.conf  postgresql.conf  start.conf

Here you can see I have the version 12, you may found it change when you will be installing it.

If you can see the config files for postgresql then it's perfectly installed ๐ŸŽ‰

Using PostgreSQL

There are some commands which are so useful for interacting with postgresql. Therefore I'm listing down them one by one here.

1. The Service Command

$ service postgresql {start|stop|restart|reload|force-reload|status}

Service command for postgresql has 6 options.

  1. start - to start the postgresql on your machine.
  2. stop- to stop the postgresql on your machine.
  3. restart - to restart postgresql.
  4. reload - for reloading the config files and much more.
  5. force-reload - for reloading forcefully.
  6. status - to check the status of database either running or stopped.
$ service postgresql status
โ— postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Sat 2020-09-05 13:36:10 PKT; 12h ago
   Main PID: 1166 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 13913)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

If you run service postgresql status, you will see the current status of postgresql. Feel free to interact with the service command.

2. The PostgreSQL Command Line Interface

For using the command line interface of postgresql, write the following commend:

$ sudo su postgres

After login, you will see the difference of terminals. Now you are logged into the command line interface of PostgreSQL.

$ sudo su postgres 
$ [sudo] password for rehan-sattar: 
$ postgres@rehansattar:/home/rehan-sattar$

After the login, write psql commend. It will open the editor shell for you to create tables, users and you will be able to write all the queries.

$ postgres@rehansattar:/home/rehan-sattar$ psql 
psql (12.4 (Ubuntu 12.4-1.pgdg20.04+1)) 
Type "help" for help.

$ postgres=# Do the PostgreSQL magic here :)

That's all! Thank you so much for reading the article โค๏ธ

ย