Step-by-Step Guide to Installing and Configuring PostgreSQL 16 from Source on Ubuntu 24.04 VPS

Introduction

At ByteHosting, we understand the importance of having a reliable and high-performance database system for your applications. PostgreSQL 16 is the latest version of this powerful open-source database, offering numerous improvements in performance, security, and features. If you're running a fresh Ubuntu 24.04 VPS and want to optimize your setup, installing PostgreSQL 16 from source is a great choice. In this step-by-step guide, we’ll walk you through the entire process, focusing on a native installation tailored for low-resource environments.

Prerequisites: VPS Setup and Required Packages

Before we begin, ensure your Ubuntu 24.04 VPS is up and running. We recommend choosing one of our cost-effective KVM VPS plans in Frankfurt, which provides the resources needed for database hosting. Once your server is ready, 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

This command installs the necessary compilers, libraries, and dependencies required for compiling PostgreSQL from source.

Downloading PostgreSQL 16 Source Code

Next, visit the official PostgreSQL website to download the latest source code. We recommend downloading the latest stable release, which at the time of writing is PostgreSQL 16.

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

After downloading, extract the archive:

tar -xzf postgresql-16.0.tar.gz

Navigate into the source directory:

cd postgresql-16.0

Compiling and Installing PostgreSQL from Source

Now, configure the build with options suitable for low-resource environments. We’ll specify the installation directory and disable unnecessary features to optimize performance:

./configure --prefix=/opt/postgresql-16 --without-readline --without-zlib

Compile the source code. You can speed up the process by using multiple cores:

make -j$(nproc)

Once compilation completes, install PostgreSQL:

sudo make install

Set up the PostgreSQL user and data directory:

sudo adduser --disabled-login --gecos "" postgres
sudo mkdir /opt/postgresql-16/data
sudo chown -R postgres:postgres /opt/postgresql-16

Switch to the postgres user and initialize the database:

sudo -i -u postgres
/opt/postgresql-16/bin/initdb -D /opt/postgresql-16/data

Configuring PostgreSQL for Performance and Security

To ensure optimal performance, edit the main configuration file:

sudo nano /opt/postgresql-16/data/postgresql.conf

Some recommended settings include:

  • shared_buffers: Set to 25% of your total RAM, e.g., shared_buffers = 256MB
  • max_connections: Adjust based on your expected load, e.g., max_connections = 20
  • effective_cache_size: Set to 50-75% of total RAM, e.g., effective_cache_size = 768MB

For security, configure the pg_hba.conf file to restrict access:

sudo nano /opt/postgresql-16/data/pg_hba.conf

Allow local connections and restrict remote access as needed:

local   all             all                                     peer
host    all             all             127.0.0.1/32            md5

Finally, enable and start the PostgreSQL server:

sudo -u postgres /opt/postgresql-16/bin/pg_ctl -D /opt/postgresql-16/data -l logfile start

Verifying the Installation and Initial Setup

Check that PostgreSQL is running:

ps aux | grep postgres

Connect to the database using the psql client:

/opt/postgresql-16/bin/psql -U postgres -d postgres

If you see the PostgreSQL prompt, your installation was successful. You can now create databases, users, and start optimizing for your specific workload.

Troubleshooting Common Installation Issues

  • Missing dependencies: Ensure all required packages are installed before compiling.
  • Permission errors: Verify ownership of data directories and correct user permissions.
  • Server not starting: Check logs in the data directory for errors and ensure the port (default 5432) is open if remote access is needed.

Conclusion

Installing PostgreSQL 16 from source on your Ubuntu 24.04 VPS gives you a tailored, high-performance database environment. By following our step-by-step guide, you can optimize your setup for low-resource environments while maintaining security and stability. At ByteHosting, we’re committed to providing reliable, affordable VPS hosting solutions to support your development needs. Whether you’re deploying a small app or a large database, our servers in Frankfurt are ready to host your PostgreSQL instance with the performance you require.

Read more