Install and Configure PostgreSQL 16 from Source on Ubuntu 24.04 VPS

Introduction

At ByteHosting, we understand that for many developers and system administrators, having maximum control over your database environment is crucial. Installing PostgreSQL 16 from source on your Ubuntu 24.04 VPS allows you to fine-tune performance, enable custom features, and optimize your setup for specific workloads. In this guide, we’ll walk you through the entire process—from prerequisites to securing your installation—so you can leverage the full power of PostgreSQL 16 on your server.

Prerequisites: VPS Setup and Required Packages

Before diving into the installation, ensure your Ubuntu 24.04 VPS is up and running. We recommend choosing one of our cost-effective KVM VPS plans, such as the Xeon 4GB or Xeon 6GB, for a reliable environment. Once your server is ready, connect via SSH:

ssh user@your-server-ip

Next, update your package list and install essential build tools:

sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev

These packages include compilers, libraries, and dependencies required for compiling PostgreSQL from source.

Download PostgreSQL 16 Source Code

Visit the official PostgreSQL website to get the latest source code. As of now, PostgreSQL 16 is the newest release. Use wget to download the tarball:

wget https://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gz

Extract the archive:

tar -xzf postgresql-16.0.tar.gz

Navigate into the source directory:

cd postgresql-16.0

Compile and Install PostgreSQL 16 from Source

Configure the build with desired options. For a standard installation, run:

./configure --prefix=/usr/local/pgsql --with-openssl

Compile the source code. This process may take several minutes depending on your VPS resources:

make -j$(nproc)

Once compilation completes successfully, install PostgreSQL:

sudo make install

Next, create a dedicated PostgreSQL user and data directory:

sudo adduser --disabled-password --gecos "" postgres
sudo mkdir /usr/local/pgsql/data
sudo chown postgres:postgres /usr/local/pgsql/data

Initialize the database cluster:

sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

Configure PostgreSQL for Optimal Performance

Adjust the postgresql.conf file for your workload. Open the configuration file:

sudo nano /usr/local/pgsql/data/postgresql.conf

Some key parameters to consider:

  • shared_buffers: Set to 25-40% of your total RAM, e.g., shared_buffers = 1GB
  • effective_cache_size: Approximate OS cache, e.g., effective_cache_size = 3GB
  • work_mem: Memory for internal sort operations, e.g., work_mem = 16MB
  • maintenance_work_mem: For vacuuming and indexing, e.g., maintenance_work_mem = 256MB

Save your changes and exit the editor.

Start and Verify PostgreSQL Service

Start the PostgreSQL server:

sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

Verify that PostgreSQL is running:

ps aux | grep postgres

You can also connect to the database using the psql client:

/usr/local/pgsql/bin/psql -U postgres

Secure Your PostgreSQL Installation

Security is vital. First, set a password for the postgres user:

sudo -u postgres /usr/local/pgsql/bin/psql -c "\\password"

Configure pg_hba.conf to control client authentication. For example, to allow local connections with password authentication, edit:

sudo nano /usr/local/pgsql/data/pg_hba.conf

Ensure only trusted networks can access your database, and disable remote access if unnecessary.

Finally, enable SSL for encrypted connections and keep your PostgreSQL version updated for security patches.

Troubleshoot Common Installation Issues

If you encounter build errors, verify all dependencies are installed. For missing libraries, install the corresponding -dev packages. For startup issues, check logs in your data directory or run:

sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data status

Remember, our VPS hosting in Frankfurt provides a stable environment for your PostgreSQL deployment, with high uptime and instant provisioning. If you need additional resources or support, our plans are flexible and designed for performance.

Conclusion

Installing PostgreSQL 16 from source on your Ubuntu 24.04 VPS gives you maximum control over your database environment. While it requires some initial setup and configuration, the benefits in performance tuning and feature access are well worth the effort. At ByteHosting, we’re committed to providing reliable, affordable infrastructure so you can focus on building your applications. Follow this guide to get your PostgreSQL 16 server up and running smoothly, and don’t hesitate to reach out if you need assistance or want to explore our VPS options for hosting your database environment.

Read more