Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu 22.04 - Install PostgreSQL

Similar Posts

Native Package - PostgreSQL 14

Server

If you wish to set up a PostgreSQL 14 database server:

sudo apt update \
  && sudo apt install postgresql-14

Client Only

If you just wish to install a client to connect to other PostgreSQL servers:

sudo apt update \
  && sudo apt install postgresql-client-14

PostgreSQL Repository

If you want to install the very latest version of PostgreSQL then you will need to add their repository which can be done with the script below:

# Download the PostgreSQL public key for checking signatures of packages
sudo curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/keyrings/pgsql.gpg
sudo chmod a+r /etc/apt/keyrings/pgsql.gpg

# Add the postgresql repository to our sources
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/pgsql.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
  | sudo tee /etc/apt/sources.list.d/pgdg.list > /dev/null

# Update the package lists:
sudo apt-get update

... then you can install whichever version you want, including version 15.

sudo apt-get -y install postgresql-15

... or if you just need the client:

sudo apt-get -y install postgresql-client-15

Check Version

If you want to check what version of PostgreSQL you are now running, use the following command:

sudo -u postgres psql postgres -c 'SELECT version()' | grep PostgreSQL

Configure It

After having installed PostgreSQL, you probably want to spend a bit of time configuring it (passwords etc).

References

Last updated: 3rd September 2023
First published: 17th July 2023

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch