How to Install PostgreSQL on Ubuntu 20.04

Install PostgreSQL on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure PostgreSQL on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

PostgreSQL is an open source, powerful, advanced, high performance and stable relational-document database system. It uses and enhances the SQL language coupled with a large number of features for secure data storage and management.

Install PostgreSQL on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install PostgreSQL on Ubuntu system.

Run the following commands to install PostgreSQL server on Ubuntu:

sudo apt install postgresql postgresql-contrib

Once the installation is completed, the PostgreSQL service will start automatically. To check the Postgres version and confirm installation run following psql command:

sudo -u postgres psql -c "SELECT version();"

Step 3. PostgreSQL Roles and Databases.

PostgreSQL supports multiple authentication methods. The most commonly used are:

  • Trust – With this method, the role can connect without a password, as long as the criteria defined in the pg_hba.conf are met.
  • Password – A role can connect by providing a password. The passwords can be stored as scram-sha-256 md5 and password (clear-text)
  • Ident – This method is only supported on TCP/IP connections. Works by obtaining the client’s operating system user name, with an optional user name mapping.
  • Peer – Same as Ident but it is only supported on local connections.

You can log in to PostgreSQL using below command:

sudo su - postgres
psql

To exit from here type following in the terminal:

\q

Step 4. Create PostgreSQL Database and Role.

  • Create a new PostgreSQL RoleThe following command will create a new role named meilana:
    sudo su - postgres -c "createuser meilana"
  • Create a new PostgreSQL DatabaseCreate a new database named meilana using the createdb command:
    sudo su - postgres -c "createdb meilanadb"
  • Grant privilegesTo grant permissions to the ramona user on the database we created in the previous step, connect to the PostgreSQL shell:
    sudo -u postgres psql

    and run the following query:

    grant all privileges on database meilanadb to meilana;

Step 5. Enable remote access to PostgreSQL server.

Use the following steps to enable remote access to the PostgreSQL server:

sudo nano /etc/postgresql/10/main/postgresql.conf

Update listen_addresses like given below:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'     ###  IP address(es) to listen on;

Now restart the PostgreSQL service with systemctl command:

sudo systemctl restart postgresql

Verify the changes with the ss utility:

ss -nlt | grep 5432
LISTEN   0         128                 0.0.0.0:5432             0.0.0.0:*
LISTEN   0         128                    [::]:5432                [::]:*

Finally update pg_hba.conf file to configure server remote connections using below examples:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# The user test_user will be able access all databases from all locations using a md5 password
host    all             meilana            0.0.0.0/0                md5

# The user test_user will be able access only the test_db from all locations using a md5 password
host    meilanadb          meilana            0.0.0.0/0                md5

# The user test_user will be able access all databases from a trusted location (192.168.43.106) without a password
host    all             meilana            192.168.77.20            trust

That’s all you need to do to install PostgreSQL on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. Don’t forget to share your valuable queries/suggestions in the below comment box & also drop your worthwhile feedback.