How to Install and Configure Postgres 14 Ubuntu 20.04

Install Postgres 14 on Ubuntu 20.04 using the PGDG apt repository, configure roles and remote access, create a superuser, and manage application users.

Postgresql is an open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. It is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley.

PostgreSQL is used as the primary data store or data warehouse for many web, mobile, geospatial, and analytics applications. PostgreSQL can store structured and unstructured data in a single product.

In this guide we are going to install Postgresql 14 in Ubuntu 20.04.

Updated July 2026. These steps still work for PostgreSQL 14, but a couple of things have changed since this was first written: Ubuntu 20.04 LTS reached the end of its standard support on 31 May 2025 (security updates now require Ubuntu Pro / ESM), and PostgreSQL 14 reaches end of life on 12 November 2026. If you are starting fresh, install on a current LTS such as Ubuntu 24.04 and consider a newer PostgreSQL release (18 is the latest). The repository setup below has been updated to use a signed keyring in place of the now-removed apt-key.

Related content:

Prerequisites

To follow along, ensure you have the following:

  1. Ubuntu 20.04 server
  2. Root access to the server or user with root access
  3. Internet access from the server
  4. Knowledge of Linux terminal

Table of content

  1. Ensure that your system packages are up to date
  2. Installing Postgres 14
  3. PostgreSQL Roles and Databases Authentication Methods
  4. Connecting to Postgres Database
  5. Configuring postgres instance for remote access
  6. User management
  7. Connecting to the instance from remote host

1. Ensure that your system packages are up to date

Let’s refresh your server’s local package index using this command:

1
sudo apt update

Then we upgrade the packages in our system with this:

1
sudo apt -y upgrade

2. Installing Postgres 14

Postgres is provided in the default Ubuntu repositories. To check the version provided, use this command:

1
sudo apt-cache search postgresql | grep postgresql

The default packages provided by the default repositories are Postgres 12. If you only need Postgres 12, you can install it with the following command. The -contrib package adds some additional utilities and functionality:

1
sudo apt install postgresql postgresql-contrib

Since we are looking to install Postgres 14, we will need to add the PostgreSQL PGDG repository that provides the packages.

First install the tools used to fetch the key, then download the PGDG repository signing key into a dedicated keyring:

1
2
3
4
sudo apt install -y curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
  --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc

Then add the PGDG repository, referencing that keyring with signed-by:

1
2
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
  | sudo tee /etc/apt/sources.list.d/pgdg.list

Older guides import this key with wget ... | sudo apt-key add -. apt-key is deprecated and has been removed from recent APT releases, so use the signed-by keyring shown above instead. If you prefer, PostgreSQL now ships an automated setup that does all of the above for you:

1
2
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh

Now let’s update the package lists:

1
sudo apt -y update

Then we install the specific version of postgres we want. Use postgresql-14 instead of postgresql:

1
sudo apt -y install postgresql-14

Once the installation is successful, Postgres 14 will be started.

Use this command to check the service status:

1
sudo systemctl status postgresql

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Thu 2021-10-14 09:32:22 UTC; 55s ago
   Main PID: 204727 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 4710)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

Oct 14 09:32:22 ip-172-26-11-229 systemd[1]: Starting PostgreSQL RDBMS...
Oct 14 09:32:22 ip-172-26-11-229 systemd[1]: Finished PostgreSQL RDBMS.

Next, let’s verify the installation by connecting to the PostgreSQL database server and checking its version. Use this command:

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

Output:

1
2
3
4
                                                              version
----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.0 (Ubuntu 14.0-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
(1 row)

The exact string will vary with the point release you get — as of this update the latest PostgreSQL 14 release is 14.23.

3. PostgreSQL Roles and Databases Authentication Methods

PostgreSQL uses a concept called roles to handle client authentication and authorization. By default, Postgres is set up to use peer authentication for local connections, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

The installation procedure created a user account called postgres that is associated with the default postgres role. In order to use PostgreSQL, you can log in to that account.

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

  • Trust – A role can connect without a password, as long as the conditions defined in 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). Since PostgreSQL 14 the default is scram-sha-256.
  • Ident – Only supported on TCP/IP connections. It works by obtaining the client’s operating system user name, with an optional user name mapping.
  • Peer – Same as Ident, but it is supported on local connections only.

4. Connecting to Postgres Database

  1. By switching to the postgres user

Switch over to the postgres account on your server by typing:

1
sudo -i -u postgres

You can now access a Postgres prompt immediately by typing:

1
2
3
4
5
$ psql
psql (14.0 (Ubuntu 14.0-1.pgdg20.04+1))
Type "help" for help.

postgres=#

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

  1. By running the command as the postgres user

Use this to run the command directly as the postgres user using sudo:

1
2
3
4
5
$ sudo -u postgres psql
psql (14.0 (Ubuntu 14.0-1.pgdg20.04+1))
Type "help" for help.

postgres=#

5. Configuring postgres instance for remote access

By default postgres is only set up to be accessed locally. This is not ideal if we want to access our server from another host. In this section we are going to configure postgres to allow access from remote hosts.

Postgres 14’s configuration files live under /etc/postgresql/14/main/. The client authentication file is /etc/postgresql/14/main/pg_hba.conf:

1
2
$ sudo file /etc/postgresql/14/main/pg_hba.conf
/etc/postgresql/14/main/pg_hba.conf: ReStructuredText file, ASCII text

Change peer identification to trust

Use this command to change peer to trust:

1
sudo sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf

This will update the line in the config file to this:

1
2
# "local" is for Unix domain socket connections only
local   all             all                                     trust

trust lets any local user connect as any role without a password. That is convenient on a throwaway lab box, but unsafe on shared or production systems — keep peer (or use scram-sha-256) there.

Add a block to allow access from everywhere

Add this content to the file /etc/postgresql/14/main/pg_hba.conf to allow all hosts password access:

1
sudo vim /etc/postgresql/14/main/pg_hba.conf

Then add this:

1
host    all             all             0.0.0.0/0                scram-sha-256

Older guides use md5 here. Since PostgreSQL 14 the default password hashing is the stronger scram-sha-256, so prefer it unless you must support very old clients. Note that 0.0.0.0/0 exposes Postgres to every host on the network — restrict this to the specific IPs or subnets that need access and keep the server behind a firewall.

Ensure PostgreSQL is listening on *

Add this line to the config here /etc/postgresql/14/main/postgresql.conf to allow postgres to listen on all hosts:

1
listen_addresses = '*'

To apply the configurations, we need to restart the postgres 14 service.

Enable and restart the postgresql server to reload the configs:

1
2
sudo systemctl restart postgresql
sudo systemctl enable postgresql

6. User management

Create Super user

It would be better if we created a superuser to administer the postgres service. This is that one user that has permissions to manage other users and databases.

Connect to the DB as the postgres role:

1
2
3
4
5
6
$ sudo -u postgres psql
could not change directory to "/root": Permission denied
psql (14.0 (Ubuntu 14.0-1.pgdg20.04+1))
Type "help" for help.

postgres=#

Create a superuser with the name root using this command:

1
CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';

Check the user if it has been created and granted the necessary privileges:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
postgres=# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root      | Superuser, Create role, Create DB                          | {}

postgres=#

Managing application users

Use this to create a database, create a user and grant that user all access to that database:

1
2
3
create database app_db_name;
create user app_user with encrypted password 'dbpassword';
grant all privileges on database app_db_name to app_user;

On PostgreSQL 15 and newer the public schema is locked down by default, so the new user may also need GRANT ALL ON SCHEMA public TO app_user; before it can create objects. On PostgreSQL 14 the grant above is enough.

Checkout this comprehensive guide on user and permission management in postgres here.

7. Connecting to the instance from remote host

Use this command to test that you can connect:

1
2
3
4
psql 'postgres://<username>:<password>@<host>:<port>/<db>?sslmode=disable'

# like
psql 'postgres://root:[email protected]:5432/postgres?sslmode=disable'

Conclusion

Up to this point we have managed to install Postgresql 14 on an Ubuntu server, do some basic configurations then do basic user management.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy