Install PostgreSQL 16

Overview

This procedure install PostgreSQL 16 onto an Ubuntu workstation using a package manager. PostgreSQL 16 will coexist with PostGreSQL 14 until the upgrade is done.

Current Status

I ran the following command to determine the status of the PostgreSQL service on my Ubuntu workstation:

systemctl status postgresql

The status is:

 postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2024-03-26 16:45:24 AEDT; 46min ago
    Process: 1629 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1629 (code=exited, status=0/SUCCESS)
        CPU: 976us

Mar 26 16:45:24 coogee systemd[1]: Starting PostgreSQL RDBMS...
Mar 26 16:45:24 coogee systemd[1]: Finished PostgreSQL RDBMS.

However, this does not give me the current version. The following command points to the possible version of PostGreSQL that I am running:

systemctl status system-postgresql.slice

The output is:

 system-postgresql.slice - Slice /system/postgresql
     Loaded: loaded
     Active: active since Tue 2024-03-26 16:44:28 AEDT; 1h 20min ago
      Tasks: 7
     Memory: 32.8M
        CPU: 1.415s
     CGroup: /system.slice/system-postgresql.slice
             └─postgresql@14-main.service
               ├─1370 /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgresq>
               ├─1538 "postgres: 14/main: checkpointer " "" "" "" "" "" "" "" ">
               ├─1539 "postgres: 14/main: background writer " "" "" "" "" "" "">
               ├─1540 "postgres: 14/main: walwriter " "" "" "" "" "" "" "" "" ">
               ├─1541 "postgres: 14/main: autovacuum launcher " "" "" "" "" "" >
               ├─1542 "postgres: 14/main: stats collector " "" "" "" "" "" "" ">
               └─1543 "postgres: 14/main: logical replication launcher " "" "" >

Notice: journal has been rotated since unit was started, output may be incomple>

That the version is 14 is confirmed by the following command:

psql --version

The output is:

psql (PostgreSQL) 14.11 (Ubuntu 14.11-0ubuntu0.22.04.1)

Install Postgres 16

I am following the procedure in "How to Install PostgreSQL 16 on Ubuntu 24.04, 22.04 or 20.04".

Import PostgreSQL APT Repository on Ubuntu

Install Initial Packages for PostgreSQL 16

As advised by the above procedure, I ran the following command to install extra packages to aid in the installation of database software:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https lsb-release curl -y

The output is:

[sudo] password for douglas: 
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
lsb-release is already the newest version (11.1.0ubuntu4).
ca-certificates is already the newest version (20230311ubuntu0.22.04.1).
curl is already the newest version (7.81.0-1ubuntu1.15).
curl set to manually installed.
dirmngr is already the newest version (2.2.27-3ubuntu2.1).
software-properties-common is already the newest version (0.99.22.9).
The following NEW packages will be installed:
  apt-transport-https
0 to upgrade, 1 to newly install, 0 to remove and 0 not to upgrade.
Need to get 1,510 B of archives.
After this operation, 170 kB of additional disk space will be used.
Get:1 http://au.archive.ubuntu.com/ubuntu jammy-updates/universe amd64 apt-transport-https all 2.4.11 [1,510 B]
Fetched 1,510 B in 1s (2,255 B/s)               
Selecting previously unselected package apt-transport-https.
(Reading database ... 417029 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.4.11_all.deb ...
Unpacking apt-transport-https (2.4.11) ...
Setting up apt-transport-https (2.4.11) ...

Add PostgreSQL 16 APT Repository

I verified that the GPG file does not exist using the following command:

sudo file /usr/share/keyrings/postgresql.gpg

The output is:

/usr/share/keyrings/postgresql.gpg: cannot open `/usr/share/keyrings/postgresql.gpg' (No such file or directory)

I used the following command to import the PostgreSQL GPG key:

curl -fSsL https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
    gpg --dearmor | \
    sudo tee /usr/share/keyrings/postgresql.gpg > /dev/null

Stable Repository for Production / General Use

I chose the production version using the following command:

echo deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main | \
    sudo tee /etc/apt/sources.list.d/postgresql.list

The output is:

[sudo] password for douglas: 
deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main

Install PostgreSQL 16 on Ubuntu via APT

First Attempt to Install PostreSQL 16

I ran the following command in attempt to install version 16 of the PostgreSQL software:

sudo apt install postgresql-client-16 postgresql-16

The following error messages were returned:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package postgresql-client-16
E: Unable to locate package postgresql-16

Diagnosing Installation Failure

Checking for package source:

grep -ir postgre /etc/apt/sources.list* | \
    grep -vE '(:#|\.save:)'

The files found are:

/etc/apt/sources.list.d/pgadmin4.list.distUpgrade:deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/focal pgadmin4 main
/etc/apt/sources.list.d/postgresql.list:deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main
/etc/apt/sources.list.d/pgdg.list.distUpgrade:deb http://apt.postgresql.org/pub/repos/apt focal-pgdg main

The sources appear to be correct.

The problem was caused by me skipping a step. I failed to update the local database. This was corrected by the following command:

sudo apt update

The result was:

[sudo] password for douglas: 
Hit:2 http://au.archive.ubuntu.com/ubuntu jammy InRelease                      
Get:3 https://cli.github.com/packages stable InRelease [3,917 B]               
Hit:4 http://au.archive.ubuntu.com/ubuntu jammy-updates InRelease              
Hit:5 http://au.archive.ubuntu.com/ubuntu jammy-backports InRelease            
Hit:1 https://packages.microsoft.com/repos/code stable InRelease               
Get:6 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]      
Ign:7 https://download.webmin.com/download/newkey/repository stable InRelease  
Hit:8 http://apt.postgresql.org/pub/repos/apt jammy-pgdg InRelease             
Hit:9 https://download.webmin.com/download/newkey/repository stable Release    
Get:10 https://esm.ubuntu.com/apps/ubuntu jammy-apps-security InRelease [7,553 B]
Ign:12 https://pkg.jenkins.io/debian-stable binary/ InRelease                  
Hit:13 https://pkg.jenkins.io/debian-stable binary/ Release                    
Get:14 https://esm.ubuntu.com/apps/ubuntu jammy-apps-updates InRelease [7,456 B]
Hit:15 https://ppa.launchpadcontent.net/sunderme/texstudio/ubuntu jammy InRelease
Get:17 https://esm.ubuntu.com/infra/ubuntu jammy-infra-security InRelease [7,450 B]
Get:18 http://security.ubuntu.com/ubuntu jammy-security/main i386 Packages [429 kB]
Get:19 https://esm.ubuntu.com/infra/ubuntu jammy-infra-updates InRelease [7,449 B]
Get:20 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1,290 kB]
Get:21 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [851 kB]
Fetched 2,713 kB in 3s (789 kB/s)                          
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
15 packages can be upgraded. Run 'apt list --upgradable' to see them.

The upgradable packages are:

Listing... Done
libdbd-pg-perl/jammy-pgdg 3.18.0-1.pgdg22.04+1 amd64 [upgradable from: 3.15.1-1]
libpq5/jammy-pgdg 16.2-1.pgdg22.04+1 amd64 [upgradable from: 14.11-0ubuntu0.22.04.1]
postgresql-14/jammy-pgdg 14.11-1.pgdg22.04+1 amd64 [upgradable from: 14.11-0ubuntu0.22.04.1]
postgresql-client-12/jammy-pgdg 12.18-1.pgdg22.04+1 amd64 [upgradable from: 12.12-1.pgdg20.04+1]
postgresql-client-14/jammy-pgdg 14.11-1.pgdg22.04+1 amd64 [upgradable from: 14.11-0ubuntu0.22.04.1]
postgresql-client-common/jammy-pgdg,jammy-pgdg,jammy-pgdg 257.pgdg22.04+1 all [upgradable from: 242.pgdg20.04+1]
postgresql-common/jammy-pgdg,jammy-pgdg,jammy-pgdg 257.pgdg22.04+1 all [upgradable from: 242.pgdg20.04+1]
postgresql-contrib/jammy-pgdg,jammy-pgdg,jammy-pgdg 16+257.pgdg22.04+1 all [upgradable from: 14+242.pgdg20.04+1]
postgresql/jammy-pgdg,jammy-pgdg,jammy-pgdg 16+257.pgdg22.04+1 all [upgradable from: 14+242.pgdg20.04+1]
python-psycopg2-doc/jammy-pgdg,jammy-pgdg,jammy-pgdg 2.9.9-1.pgdg22.04+1 all [upgradable from: 2.9.2-1build2]
python3-psycopg2/jammy-pgdg 2.9.9-1.pgdg22.04+1 amd64 [upgradable from: 2.9.2-1build2]
thunderbird-gnome-support/jammy-security 1:115.9.0+build1-0ubuntu0.22.04.1 amd64 [upgradable from: 1:115.8.1+build1-0ubuntu0.22.04.1]
thunderbird-locale-en-us/jammy-security,jammy-security 1:115.9.0+build1-0ubuntu0.22.04.1 all [upgradable from: 1:115.8.1+build1-0ubuntu0.22.04.1]
thunderbird-locale-en/jammy-security 1:115.9.0+build1-0ubuntu0.22.04.1 amd64 [upgradable from: 1:115.8.1+build1-0ubuntu0.22.04.1]
thunderbird/jammy-security 1:115.9.0+build1-0ubuntu0.22.04.1 amd64 [upgradable from: 1:115.8.1+build1-0ubuntu0.22.04.1]

Most of the updates are for older versions of Postgres and associated software. I updated the software as follows:

sudo apt -y upgrade >update_all_packages.log 2>&1

postgresql-client-16 was installed automatically.

Second Attempt to Install PostreSQL 16

I ran the following command in attempt to install version 16 of the PostgreSQL software:

sudo apt -y install postgresql-16 >install_postgres_16.log 2>&1

Status after Postgres 16 Installation

After installation, I used the following command to display the current status:

systemctl status system-postgresql.slice

The output was:

 system-postgresql.slice - Slice /system/postgresql
     Loaded: loaded
     Active: active since Tue 2024-03-26 16:44:28 AEDT; 6h ago
      Tasks: 13
     Memory: 60.7M
        CPU: 15.416s
     CGroup: /system.slice/system-postgresql.slice
             ├─postgresql@14-main.service
             │ ├─40899 /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgres>
             │ ├─40901 "postgres: 14/main: checkpointer " "" "" "" "" "" "" "" >
             │ ├─40902 "postgres: 14/main: background writer " "" "" "" "" "" ">
             │ ├─40903 "postgres: 14/main: walwriter " "" "" "" "" "" "" "" "" >
             │ ├─40904 "postgres: 14/main: autovacuum launcher " "" "" "" "" "">
             │ ├─40905 "postgres: 14/main: stats collector " "" "" "" "" "" "" >
             │ └─40906 "postgres: 14/main: logical replication launcher " "" "">
             └─postgresql@16-main.service
               ├─46844 /usr/lib/postgresql/16/bin/postgres -D /var/lib/postgres>
               ├─46845 "postgres: 16/main: checkpointer " "" "" "" "" "" "" "" >
               ├─46846 "postgres: 16/main: background writer " "" "" "" "" "" ">
               ├─46848 "postgres: 16/main: walwriter " "" "" "" "" "" "" "" "" >
               ├─46849 "postgres: 16/main: autovacuum launcher " "" "" "" "" "">
               └─46850 "postgres: 16/main: logical replication launcher " "" "">

Notice: journal has been rotated since unit was started, output may be incomple>
lines 1-24/24 (END)

References