Deploying PostgreSQL on Ubuntu 24.04
Introduction
This is a quick post on how to install PostgreSQL on Ubuntu.
Parts of this document are copied from the official PostgreSQL documentation at https://www.postgresql.org/download/linux/ubuntu/ for my own reference.
Repository configuration
At the time of writing, the PostgreSQL Apt repository supports the following Ubuntu LTS versions:
| Name | Version | Architectures |
|---|---|---|
| noble | 24.04, LTS | amd64, arm64 |
| jammy | 22.04, LTS | amd64, arm64 |
| focal | 20.04, LTS | amd64, arm64 |
It is possible to configure the apt repository either automatically or manually. We’ll use the scripts from the GitHub repository iac-scripts from CoreLayer, which combines the manual configuration steps in a simple shell script.
GitHub Repository
git clone https://github.com/corelayer/iac-scripts /tmp/iac-scripts
sudo sh /tmp/iac-scripts/linux/software/postgresql/apt-configure.sh
Automatic configuration
sudo apt install -u postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
Manual configuration
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt update
sudo apt -y install postgresql
Server installation
PostgreSQL 16
To install the core database server, we can use the script apt-install_postgresql-16.sh from the same repository:
sudo sh /tmp/iac-scripts/linux/software/postgresql/apt-install_postgresql-16.sh
Server configuration
The installation command run in the step above, will install and configure PostgreSQL to run on a specific user postgres with the correct privileges on Ubuntu.
However, additional steps need to be taken to configure the server.
The configuration file is located in /etc/postgresql/$version/main/postgresql.conf.
Listen address
By default, PostgreSQL listens on localhost only, port tcp/5432.
First of all, we’ll change that into the IP address of our server.
sudo sh /tmp/iac-scripts/linux/software/postgresql/configure/listenaddress.sh $version $ipaddress
