This guide will walk you through the steps used to install PostgreSQL 12 on Ubuntu 22.04/20.04/18.04/16.04 Linux system. PostgreSQL is one of the most widely adopted object-relational database management system based on POSTGRES 4.2. PostgreSQL 12 has been released for general use, fit for Production and all Development use cases.
For CentOS 7/8: How To Install PostgreSQL 12 on CentOS 7 / CentOS 8
For Fedora: Installing PostgreSQL 12 on Fedora
Debian: Install PostgreSQL 12 on Debian
If you want to see all the new features and improvements in PostgreSQL 12, visit the PostgreSQL 12 release notes page so check the major enhancements in PostgreSQL 12. Without much wait, let’s buckle to the installation of PostgreSQL 12 on Ubuntu 22.04/20.04/18.04/16.04 Linux system.
Step 1: Update system
It is recommended to update your current system packages if it is a new server instance.
sudo apt update
sudo apt -y install vim bash-completion wget
sudo apt -y upgrade
A reboot is necessary after an upgrade.
sudo reboot
Step 2: Add PostgreSQL 12 repository
We need to import GPG key and add PostgreSQL 12 repository into our Ubuntu machine. Run the following commands to accomplish this.
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
After importing GPG key, add repository contents to your Ubuntu 22.04/20.04/18.04/16.04 system:
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
The repository added contains many different packages including third party addons. They include:
- postgresql-client
- postgresql
- libpq-dev
- postgresql-server-dev
- pgadmin packages
Step 3: Install PostgreSQL 12 on Ubuntu
Now the repository has been added successfully, update the package list and install PostgreSQL 12 server and client packages on your Ubuntu 22.04/20.04/18.04/16.04 Linux system.
sudo apt update
sudo apt -y install postgresql-12 postgresql-client-12
A successful installation prints a message that is similar to one shared in the next screenshot.
The PostgreSQL service is started and set to come up after every system reboot.
$ systemctl status postgresql.service
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2022-05-18 15:50:22 EAT; 15min ago
Main PID: 4317 (code=exited, status=0/SUCCESS)
CPU: 1ms
Mei 18 15:50:22 ubuntu22 systemd[1]: Starting PostgreSQL RDBMS...
Mei 18 15:50:22 ubuntu22 systemd[1]: Finished PostgreSQL RDBMS.
$ systemctl status [email protected]
● [email protected] - PostgreSQL Cluster 12-main
Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
Active: active (running) since Wed 2022-05-18 16:04:01 EAT; 1min 59s ago
Process: 6838 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 12-main start (code=exited, status=0/SUCCESS)
Main PID: 6843 (postgres)
Tasks: 7 (limit: 9460)
Memory: 18.2M
CPU: 157ms
CGroup: /system.slice/system-postgresql.slice/[email protected]
├─6843 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf
├─6845 "postgres: 12/main: checkpointer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
├─6846 "postgres: 12/main: background writer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
├─6847 "postgres: 12/main: walwriter " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
├─6848 "postgres: 12/main: autovacuum launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
├─6849 "postgres: 12/main: stats collector " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
└─6850 "postgres: 12/main: logical replication launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
Mei 18 16:03:58 ubuntu22 systemd[1]: Starting PostgreSQL Cluster 12-main...
Mei 18 16:04:01 ubuntu22 systemd[1]: Started PostgreSQL Cluster 12-main.
$ systemctl is-enabled postgresql
enabled
Step 4: Test PostgreSQL Connection
During installation, a postgres user is created automatically. This user has full superadmin access to your entire PostgreSQL instance. Before you switch to this account, your logged in system user should have sudo privileges.
sudo su - postgres
Let’s reset this user password to a strong Password we can remember.
psql -c "alter user postgres with password 'StrongAdminPassw0rd'"
Start PostgreSQL prompt by using the command:
$ psql
Get connection details like below.
$ psql
psql (12.11 (Ubuntu 12.11-1.pgdg22.04+1))
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
Let’s create a test database and user to see if it’s working.
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# CREATE USER mytestuser WITH ENCRYPTED PASSWORD 'MyStr0ngP@SS';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE mytestdb to mytestuser;
GRANT
List created databases:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-------------------------
mytestdb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | mytestuser=CTc/postgres
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Connect to database:
postgres-# \c mytestdb
You are now connected to database "mytestdb" as user "postgres".
Other PostgreSQL utilities installed such as createuser and createdb can be used to create database and users.
postgres@ubuntu:~$ createuser myuser --password
Password:
postgres@ubuntu:~$ createdb mydb -O myuser
postgres@ubuntu:~$ psql -l
We can create and connect to a database on PostgreSQL server.
Step 5: Configure remote Connection (Optional)
Installation of PostgreSQL 12 on Ubuntu only accepts connections from localhost. In ideal production environments, you’ll have a central database server and remote clients connecting to it – But of course within a private network (LAN).
To enable remote connections, edit PostgreSQL configuration file:
sudo nano /etc/postgresql/12/main/postgresql.conf
Uncomment line 59 and change the Listen address to accept connections within your networks.
# Listen on all interfaces
listen_addresses = '*'
# Listen on specified private IP address
listen_addresses = '192.168.10.11'
After the change, restart postgresql service.
sudo systemctl restart postgresql
Confirm Listening addresses.
$ sudo netstat -tunelp | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 111 112837 11143/postgres
tcp6 0 0 :::5432 :::* LISTEN 111 112838 11143/postgres
Step 6: Install pgAdmin4 Management Tool
If you want to manage your PostgreSQL database server from a web interface, then install pgAdmin4.
Enjoy using PostgreSQL 12 on Ubuntu 22.04|20.04|18.04|16.04 Linux system.
Recommended books:
Other guides related to databases are shared in the list below.
How to Install PostGIS on Ubuntu
How to solve “MySQL server is running with the –secure-file-priv” Error
How to Install pgAdmin 4 on Debian
How to Install pgAdmin 4 on CentOS 7
How to Install pgAdmin4 on FreeBSD 12
How to Install PostGIS on CentOS 7
Tags:
- Install PostgreSQL 12 on Ubuntu 20.04/18.04
- Install PostgreSQL 12 on Ubuntu 16.04
- Install PostgreSQL 12 on Ubuntu Linux
- PostgreSQL 12 installation on Ubuntu 20.04/18.04/16.04
- PostgreSQL 12 installation on Ubuntu 20.04