Install PostgreSQL on Ubuntu 22.04

This article is going to teach you how to Install PostgreSQL on Ubuntu 22.04. PostgreSQL, commonly known as Postgres, is a relational database management system (RDBMS) that focuses on flexibility and SQL compliance. SQL (relational) and JSON (non-relational) querying are both supported by PostgreSQL.

Transactions with Atomicity, Consistency, Isolation, and Durability (ACID) qualities, automatically updatable views, materialized views, triggers, foreign keys, and stored procedures are among PostgreSQL’s features. It can manage a wide range of workloads, from single computers to data warehouses or Web services with a large number of concurrent users. It is also available for Windows, Linux, FreeBSD, and OpenBSD, and is the default database for macOS Server.

How to Install PostgreSQL on Ubuntu 22.04

  • To Install PostgreSQL on Ubuntu 22.04, begin by adding the GPG key for repository signing to your machine using the following command.
wget -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  • Then, on your Ubuntu 22.04 system, generate a PPA file for PostgreSQL using the command below.
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' 
  • Run system update to update changes.
sudo apt update 
  • After updating changes in your system, run the following command to install latest version of PostgreSQL server.
sudo apt-get install postgresql postgresql-contrib 

Sample output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  galera-4 libaio1 libconfig-inifiles-perl
  libdbd-mariadb-perl libfprint-2-tod1
  libhtml-template-perl libmariadb3 libpmem1
  libreadline5 libterm-readkey-perl
  mariadb-client-10.7 mariadb-client-core-10.7
  mariadb-common mariadb-server-10.7
  mariadb-server-core-10.7 socat
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libpq5 pgdg-keyring postgresql-14
  postgresql-client-14 postgresql-client-common
  postgresql-common sysstat
Suggested packages:
  postgresql-doc postgresql-doc-14 isag
The following NEW packages will be installed:
  libpq5 pgdg-keyring postgresql postgresql-14
  postgresql-client-14 postgresql-client-common
  postgresql-common postgresql-contrib sysstat
0 upgraded, 9 newly installed, 0 to remove and 8 not upgraded.
Need to get 18.4 MB of archives.
After this operation, 61.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 libpq5 amd64 14.2-1.pgdg20.04+1 [170 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 sysstat amd64 12.2.0-2ubuntu0.1 [448 kB]
Get:3 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 pgdg-keyring all 2018.2 [10.7 kB]
Get:4 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql-client-common all 238.pgdg20.04+1 [92.0 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql-client-14 amd64 14.2-1.pgdg20.04+1 [1,614 kB]
Get:6 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql-common all 238.pgdg20.04+1 [229 kB]
Get:7 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql-14 amd64 14.2-1.pgdg20.04+1 [15.7 MB]
Get:8 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql all 14+238.pgdg20.04+1 [66.5 kB]
Get:9 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 postgresql-contrib all 14+238.pgdg20.04+1 [66.5 kB]
Fetched 18.4 MB in 17s (1,072 kB/s)                 
Preconfiguring packages ...
Selecting previously unselected package libpq5:amd64.
(Reading database ... 202793 files and directories currently installed.)
Preparing to unpack .../0-libpq5_14.2-1.pgdg20.04+1_amd64.deb ...
Unpacking libpq5:amd64 (14.2-1.pgdg20.04+1) ...
Selecting previously unselected package pgdg-keyring.
Preparing to unpack .../1-pgdg-keyring_2018.2_all.deb ...
Unpacking pgdg-keyring (2018.2) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../2-postgresql-client-common_238.pgdg20.04+1_all.deb ...
Unpacking postgresql-client-common (238.pgdg20.04+1) ...
Selecting previously unselected package postgresql-client-14.
Preparing to unpack .../3-postgresql-client-14_14.2-1.pgdg20.04+1_amd64.deb ...
Unpacking postgresql-client-14 (14.2-1.pgdg20.04+1) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../4-postgresql-common_238.pgdg20.04+1_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by postgresql-common'
Unpacking postgresql-common (238.pgdg20.04+1) ...
Selecting previously unselected package postgresql-14.
Preparing to unpack .../5-postgresql-14_14.2-1.pgdg20.04+1_amd64.deb ...
Unpacking postgresql-14 (14.2-1.pgdg20.04+1) ...
Selecting previously unselected package postgresql.
Preparing to unpack .../6-postgresql_14+238.pgdg20.04+1_all.deb ...
Unpacking postgresql (14+238.pgdg20.04+1) ...
Selecting previously unselected package postgresql-contrib.
Preparing to unpack .../7-postgresql-contrib_14+238.pgdg20.04+1_all.deb ...
Unpacking postgresql-contrib (14+238.pgdg20.04+1) ...
Selecting previously unselected package sysstat.
Preparing to unpack .../8-sysstat_12.2.0-2ubuntu0.1_amd64.deb ...
Unpacking sysstat (12.2.0-2ubuntu0.1) ...
Setting up pgdg-keyring (2018.2) ...
Removing apt.postgresql.org key from trusted.gpg: OK
Setting up libpq5:amd64 (14.2-1.pgdg20.04+1) ...
Setting up sysstat (12.2.0-2ubuntu0.1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up postgresql-client-common (238.pgdg20.04+1) ...
Setting up postgresql-client-14 (14.2-1.pgdg20.04+1) ...
update-alternatives: using /usr/share/postgresql/14/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-common (238.pgdg20.04+1) ...
Adding user postgres to group ssl-cert

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
  en_us
Removing obsolete dictionary files:
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /lib/systemd/system/postgresql.service.
Setting up postgresql-14 (14.2-1.pgdg20.04+1) ...
Creating new PostgreSQL cluster 14/main ...
/usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main --auth-local peer --auth-host scram-sha-256 --no-instructions
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/14/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Africa/Nairobi
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
update-alternatives: using /usr/share/postgresql/14/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Setting up postgresql-contrib (14+238.pgdg20.04+1) ...
Setting up postgresql (14+238.pgdg20.04+1) ...
Processing triggers for systemd (245.4-4ubuntu3.15) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
  • PostgreSQL should be running, check it status using the command below.
sudo systemctl status postgresql 

Sample output

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.>
     Active: active (exited) since Fri 2022-02-25 22>
   Main PID: 28474 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 4588)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

Feb 25 22:56:26 ubuntu systemd[1]: Starting PostgreS>
Feb 25 22:56:26 ubuntu systemd[1]: Finished PostgreS>
  • You can stop PostgreSQL using the following command.
sudo systemctl stop postgresql.service
  • To start PostgreSQL run the command below.
sudo systemctl start postgresql.service
  • In case you want PostgreSQL to run automatically when you boot your system, enable it using the following command.
sudo systemctl enable postgresql.service

Secure PostgreSQL on Ubuntu 22.04

  • The user “postgres” is created by default in the PostgreSQL installation and is not password secured. To begin, run the following command to generate a password for the “postgres” user account.
sudo passwd postgres
  • Using the Postgres system account, log in by running the command below.
su - postgres 
  • Next, for the PostgreSQL administrator user, set a strong and secure password. Note, your-strong-password should be replaced with a secure password for the administrator database user.
psql -c "ALTER USER postgres WITH PASSWORD 'your-strong-password';"
  • Then exit by simply typing;
exit
  • To apply the security adjustments, restart the service using the following command.
sudo systemctl restart postgresql
  • You have reached the end of the article, Congratulations. You have learned how to Install PostgreSQL on Ubuntu 22.04.

Other Tutorials

Install MariaDB 10.7 on CentOS 7

Install PHP 7.4 on Ubuntu 18.04

Install MariaDB 10.7 on Ubuntu 18.04

System administrator | Software Developer | DevOps

Leave a Comment