Installation Guides 4d ago 5 views 5 min read

How to install PostgreSQL 16 on Ubuntu 24.04

Follow these steps to install the latest PostgreSQL 16 release on a fresh Ubuntu 24.04 server. This guide covers adding the official repository, configuring parameters, and securing the database server for production use.

Roy S
Updated 7h ago
Sponsored

Cloud VPS — scale in minutes

Instantly deploy SSD cloud VPS with guaranteed resources, snapshots and per-hour billing. Pay only for what you use.

You will install PostgreSQL 16 on a fresh Ubuntu 24.04 server using the official upstream repository. This process ensures you get the latest stable version, version 16.5, along with essential development libraries. The guide assumes you have root access or sudo privileges to run the necessary commands.

Prerequisites

  • Ubuntu 24.04 LTS (Noble Numbat) running on x86_64 or ARM64 architecture.
  • Root access or a user account with sudo privileges.
  • At least 512 MB of free RAM for the database server.
  • Active internet connection to download packages and repository keys.

Step 1: Update system packages and add the PostgreSQL APT repository

Begin by updating your package index to ensure you have the latest security patches. Next, add the official PostgreSQL GPG key to verify package authenticity. Finally, add the repository configuration to allow you to install the latest version of PostgreSQL 16.

sudo apt update
sudo apt install -y gnupg curl
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

After running these commands, verify the repository was added correctly by checking the sources list. You should see the new pgdg.list file containing the main repository URL for your specific Ubuntu release.

cat /etc/apt/sources.list.d/pgdg.list
deb http://apt.postgresql.org/pub/repos/apt noble-pgdg main
deb http://apt.postgresql.org/pub/repos/apt noble-pgdg contrib
deb http://apt.postgresql.org/pub/repos/apt noble-pgdg contrib
deb http://apt.postgresql.org/pub/repos/apt noble-pgdg main

Step 2: Install PostgreSQL 16 and required libraries

Install the PostgreSQL server package along with the development headers and libraries. This step pulls in version 16.5 of the database engine, the command-line client, and the necessary build tools. The installation will also configure the initial database cluster automatically.

sudo apt install -y postgresql-16 postgresql-contrib postgresql-client-16 libpq-dev

Once the installation finishes, you will see a message indicating that the database cluster was created and the service started successfully. The default installation configures PostgreSQL to listen on port 5432 and creates the default postgres user.

Step 3: Configure PostgreSQL parameters

Edit the main configuration file to adjust memory limits and connection settings for your workload. Locate the file at /etc/postgresql/16/main/postgresql.conf and modify the shared_buffers setting to use 256MB. Increase the work_mem and maintenance_work_mem values to optimize sorting and indexing operations.

sudo nano /etc/postgresql/16/main/postgresql.conf

Find the lines for shared_buffers, work_mem, and maintenance_work_mem. Remove the comment hash symbol (#) at the start of these lines and set the values as shown below. Save the file and exit the text editor.

shared_buffers = 256MB
work_mem = 16MB
maintenance_work_mem = 512MB

Also, ensure the listen_addresses parameter is set to '*' if you want to accept connections from other servers. Find the line and change it from 'localhost' to '*' or remove the line entirely to use the default setting.

Step 4: Configure pg_hba.conf for authentication

Modify the host-based authentication file to allow connections from your application servers. Open the file located at /etc/postgresql/16/main/pg_hba.conf. Replace the default 'peer' authentication method with 'md5' or 'scram-sha-256' for remote connections.

sudo nano /etc/postgresql/16/main/pg_hba.conf

Add a line at the end of the file to allow connections from any IP address using password authentication. Set the type to 'host' to allow TCP/IP connections, the database to 'all', the user to 'all', and the method to 'md5'. Save the changes.

host    all             all             0.0.0.0/0               md5

Restart the PostgreSQL service to apply the new configuration changes. The service will reload the settings without dropping existing connections.

sudo systemctl restart postgresql

Verify the installation

Connect to the database using the command-line client to verify that the installation is working correctly. Use the psql command to log in as the postgres user and run a simple query. If the output displays the PostgreSQL version, the installation is successful.

sudo -u postgres psql -c "SELECT version();"

The command should return a single row containing the version string. You should see output similar to: PostgreSQL 16.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0, 64-bit.

Troubleshooting

If the service fails to start, check the system logs for specific error messages. Use the journalctl command to view the logs for the postgresql service. Look for errors related to missing directories, permission issues, or configuration syntax errors. Common issues include the postgres user not being able to write to the data directory.

sudo journalctl -u postgresql -f

Another common problem is the listen_addresses setting. If the server only accepts local connections, ensure the listen_addresses parameter in postgresql.conf is set to '*' or left as default. If you changed the configuration file, you must restart the service for changes to take effect.

sudo systemctl restart postgresql

If authentication fails, verify the pg_hba.conf file syntax. An invalid line can prevent the service from starting. Use the validate-command option in the apt package to check syntax before restarting. Finally, ensure the pg_hba.conf file has the correct permissions set to 0640.

sudo chown postgres:postgres /etc/postgresql/16/main/pg_hba.conf
sudo chmod 0640 /etc/postgresql/16/main/pg_hba.conf

Check the firewall settings to ensure port 5432 is open for incoming connections. Use ufw to allow traffic on this specific port if you are using the Uncomplicated Firewall. Without this rule, remote clients will be blocked from connecting to the database server.

sudo ufw allow 5432/tcp

Ensure the postgres user owns the data directory. If permissions are incorrect, the service will fail to start. Reset the ownership of the entire PostgreSQL directory tree to the postgres user.

sudo chown -R postgres:postgres /var/lib/postgresql/16

Always restart the service after making configuration changes to ensure the new settings are loaded. Use the systemctl command to manage the service state. If you need to stop the service for maintenance, use the stop command. If you need to enable the service to start on boot, use the enable command.

Sponsored

Powerful Dedicated Servers — Linux & Windows

Bare-metal performance with SSD storage, DDoS protection and 24/7 expert support. Ideal for production workloads, databases and high-traffic sites.

Tags: LinuxDevOpsUbuntuDatabasePostgreSQL
0
Was this helpful?

Related tutorials

Comments 0

Login to leave a comment.

No comments yet — be the first to share your thoughts.