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, the latest version of this powerful open-source database, offers numerous improvements and new features that can significantly enhance your data management capabilities. If you're running a fresh Ubuntu 24.04 VPS and want to leverage the full potential of PostgreSQL 16, installing it from source is a great way to ensure you get the latest features and optimal performance.

In this step-by-step guide, we will walk you through the process of installing and configuring PostgreSQL 16 from source on your Ubuntu 24.04 VPS. Whether you're a developer, system administrator, or hobbyist, our instructions are designed to be clear, practical, and beginner-friendly, while still providing the technical details needed for a successful setup.

Prerequisites: Setting up Your Ubuntu 24.04 VPS

Before we begin, ensure your VPS is running Ubuntu 24.04 and that you have root or sudo privileges. It's also a good idea to update your system packages to the latest versions:

sudo apt update && sudo apt upgrade -y

This ensures your system is secure and has the latest dependencies needed for building software from source.

Downloading PostgreSQL 16 Source Code

Next, we need to download the PostgreSQL 16 source code from the official repository. Visit the PostgreSQL official website or use wget to fetch the latest release:

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 extracted directory:

cd postgresql-16.0

Compiling and Installing PostgreSQL 16 from Source

Before compiling, install the necessary dependencies:

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

Configure the build environment:

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

Compile the source code. This process may take some time depending on your VPS hardware:

make -j$(nproc)

Once compilation completes successfully, install PostgreSQL:

sudo make install

After installation, create a dedicated PostgreSQL user and initialize the database cluster:

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

Basic Configuration and Security Hardening

Configure PostgreSQL to start on system boot and set up basic security:

sudo nano /etc/systemd/system/postgresql.service

Add the following content:

[Unit]
Description=PostgreSQL 16 database server
After=network.target

[Service]
Type=simple
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable postgresql
sudo systemctl start postgresql

Secure your PostgreSQL installation by editing the pg_hba.conf and postgresql.conf files to control access and optimize performance. For example, you might restrict access to localhost initially:

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

Set the authentication method to md5 for password protection:

host    all             all             127.0.0.1/32            md5

Verifying the Installation and Initial Setup

To verify that PostgreSQL 16 is installed correctly, switch to the postgres user and connect to the database:

sudo -u postgres /usr/local/pgsql/bin/psql -c "SELECT version();"

You should see output indicating PostgreSQL 16.0 is running. From here, you can create databases, users, and configure your environment for production use.

Troubleshooting Common Installation Issues

If you encounter issues during installation, check the following:

  • Ensure all dependencies are installed correctly.
  • Verify that the build process completes without errors.
  • Check the PostgreSQL logs for detailed error messages.
  • Make sure the system has sufficient resources and permissions.

At ByteHosting, we recommend testing your PostgreSQL setup thoroughly before deploying it in a production environment. Our VPS plans provide the reliable infrastructure needed for hosting your database servers with optimal performance.

Conclusion

Installing PostgreSQL 16 from source on Ubuntu 24.04 gives you the flexibility to customize your database environment and access the latest features. By following our step-by-step guide, you can set up a secure, high-performance PostgreSQL server tailored to your needs. If you need a cost-effective VPS to host your PostgreSQL database, explore our hosting plans in Frankfurt, Germany, and experience reliable, instant deployment with 99% uptime. As always, our team at ByteHosting is here to support your hosting and infrastructure needs.

Read more