Skip to main content

Create a PostgreSQL server on Ubuntu for beginers

This guide demonstrates how to install PostgreSQL on Ubuntu 20.04 server.

PostgreSQL is a fully featured database management system (DBMS) with a strong emphasis on extensibility and SQL compliance. It is backed by 20 years of open-source development, and supports both SQL (relational) and JSON (non-relational) querying.

PostgreSQL is one of the most popular databases in the industry that is used for various web, mobile and analytics applications. Let‘s now go through a step-by-step guide of how to install PostgreSQL on Ubuntu 20.04 machine, as well as how to complete the PostgreSQL server setup.

Setup PostgreSQL

Install PostgeSQL

Add Official Repository

You may want to install PostgreSQL from an official repository, since it is updated more frequently than official Ubuntu sources.

First, you should install prerequisite software packages that will be used to download and install software certificates for a secure SSL connection.

sudo apt install wget ca-certificates -y

Then, get the certificate, add it to apt-key management utility and create a new configuration file with an official PostgreSQL repository address inside.

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

Install PostgreSQL

It is always a good idea to download information about all packages available for installation from your configured sources before the actual installation.

sudo apt update

Now is the time to do the actual PostgreSQL installation. This will install the latest PostgreSQL version along with the newest extensions and additions that are not yet officially part of the PostgreSQL core.

apt install postgresql postgresql-contrib -y

Check PostgreSQL status After the installation you may double-check that postgresql daemon is active.

systemctl status postgresql

Start Using PostgreSQL

When you install PostgreSQL a default admin user postgres is created by the default. You must use it to log-in to your PostgreSQL database for the first time.

A psql command-line client tool is used to interact with the database engine. You should invoke it as a postgres user to start an interactive session with your local database.

sudo -u postgres psql

In addition to creating a postgres admin user for you, PostgreSQL installation also creates a default database named postgres and connects you to it automatically when you first launch psql.

After first launching psql, you may check the details of your connection by typing \conninfo into the interpreter.

You are now connected to database postgres as user postgres. If you want to see a list of all the databases that are available on a server, use\l` command.

And to see a list of all the users with their privileges use \du command.

Since the default postgres user does not have a password, you should set it yourself.

\password postgres

Work on PostgreSQL

Create and Populate a New Database

You are now connected to your database server through psql command line tool with full access rights, so it’s time to create a new database.

CREATE DATABASE myshop;

After the new myshop database is created, connect to it.

\c myshop

Now you are ready to start creating tables where your data will be stored. Let’s create your first table with a primary key, and three client attributes.

CREATE TABLE clients (id SERIAL PRIMARY KEY, first_name VARCHAR, last_name VARCHAR, role VARCHAR);

You may double check that your new table is created successfully by typing a \dt command.

\dt

Let’s now insert the first row into your newly created clients table.

INSERT INTO clients (first_name, last_name, role) VALUES ('John', 'Doe', 'Level 2');

And query the table to get all its rows.

SELECT * FROM clients;

Exit to OS

\q

Setup server

Setup PostgreSQL server

It’s fun to play with the database locally, but eventually you will need to connect to it through a remote server.

When you install a PostgreSQL server, it is only accessible locally through the loopback IP address of your machine. However, you may change this setting in the PostgreSQL configuration file to allow remote access.

Let’s now exit the interactive psql session by typing exit, and access postgresql.conf configuration file of PostgreSQL version 14 by using vim text editor.

nano /etc/postgresql/16/main/postgresql.conf

Uncomment and edit the listen_addresses attribute to start listening to start listening to all available IP addresses.

listen_addresses = '*'

Now edit the PostgreSQL access policy configuration file.

nano /etc/postgresql/16/main/pg_hba.conf

Append a new connection policy (a pattern stands for [CONNECTION_TYPE] [DATABASE] [USER] [ADDRESS] [METHOD]) in the bottom of the file.

host all all 0.0.0.0/0 md5

We are allowing TCP/IP connections (host) to all databases (all) for all users (all) with any IPv4 address (0.0.0.0/0) using an MD5 encrypted password for authentication (md5).

It is now time to restart your PostgreSQL service to load your configuration changes.

systemctl restart postgresql

And make sure your system is listening to the 5432 port that is reserved for PostgreSQL.

ss -nlt | grep 5432

Connect to PostgreSQL database through a remote host

Your PostgreSQL server is now running and listening for external requests. It is now time to connect to your database through a remote host.

Connect via Command Line Tool

A psql command line tool also allows you to connect to a remote database. If you don’t have it on your remote machine yet, follow the steps 1 – 3 for a full PostgreSQL installation or install a command line tool only by using sudo apt install postgresql-client command.

You may now connect to a remote database by using the following command pattern:

psql -h [ip address] -p [port] -d [database] -U [username]

Let’s now connect to a remote PostgreSQL database that we have hosted on our machine.

psql -h localhost -p 5432 -d sample -U postgres

To double check your connection details use the \conninfo command.

Double check PostgreSQL session

Now you can start writing SQL queries to retrieve data from your database tables.

SELECT * FROM clients;

We can see that our previously created entry is safely stored in the clients table.

Exit

\q

Great, you have learned how to install PostgreSQL !