How To Install PostgreSQL 16 on Ubuntu 22.04

If you want to install latest version of Postgres on Ubuntu 20.04 maybe you’ve tried script from Postgres website, but when installing you’ll get warnings that using apt-key is deprecated in such a case and also for me not to modify the script every time I’ve decided to write it here.

# Install the prerequisites:
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

# Import the repository signing key:
curl | gpg --dearmor \
 | sudo tee /usr/share/keyrings/postgresql-archive-keyring.gpg >/dev/null

# Create the file repository configuration:
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/postgresql-archive-keyring.gpg] $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Update the package lists:
sudo apt update

# Install the latest version of PostgreSQL.
# Or any other version:
sudo apt -y install postgresql-16 postgresql-client-16