Manual Installation and Configuration of PostgreSQL 16 on Ubuntu 24.04 VPS from Source
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. While many users opt for pre-built packages or Docker containers, some prefer to compile and install PostgreSQL from source for maximum control and optimization. In this guide, we will walk you through the step-by-step process of installing PostgreSQL 16 on Ubuntu 24.04 VPS from source. Whether you're running a high-load application or just want to learn more about PostgreSQL internals, this tutorial is designed to be clear, practical, and beginner-friendly.
1. Prerequisites: Setting up your Ubuntu 24.04 VPS and required dependencies
Before we begin, ensure your Ubuntu 24.04 VPS is up and running. We recommend choosing one of our cost-effective VPS plans, such as the Ryzen Virtual Servers or Xeon-based options, to get the best performance and reliability. Once your server is ready, connect via SSH:
ssh user@your-server-ipNext, update your system packages to ensure everything is current:
sudo apt update && sudo apt upgrade -yInstalling the essential dependencies is crucial for compiling PostgreSQL from source. Run the following command to install the necessary build tools and libraries:
sudo apt install -y build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-devThese packages include compilers, libraries, and tools required for building PostgreSQL and its optional modules.
2. Downloading PostgreSQL 16 source code from the official repository
Now, let's fetch the latest PostgreSQL 16 source code. We will use wget to download the tarball from the official PostgreSQL website:
wget https://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gzAfter downloading, extract the archive:
tar -xzf postgresql-16.0.tar.gzNavigate into the source directory:
cd postgresql-16.03. Compiling and installing PostgreSQL 16 from source
Before compiling, it's recommended to create a dedicated user for running PostgreSQL:
sudo adduser --disabled-login --gecos "" postgresSwitch to the new user:
sudo -i -u postgresConfigure the build with desired options. For example, to specify the installation directory and enable certain features:
./configure --prefix=/opt/postgresql-16 --with-openssl --with-libxml --with-libxsltCompile the source code. This process may take several minutes depending on your VPS's hardware:
make -j$(nproc)Once compilation completes successfully, install PostgreSQL:
sudo make installAfter installation, initialize the database cluster:
/opt/postgresql-16/bin/initdb -D /opt/postgresql-16/data4. Configuring PostgreSQL for optimal performance and security
Next, we need to configure PostgreSQL to suit our needs. First, create a systemd service file for easy management:
sudo nano /etc/systemd/system/postgresql-16.serviceInsert the following content:
[Unit]
Description=PostgreSQL 16 database server
After=network.target
[Service]
Type=forking
User=postgres
ExecStart=/opt/postgresql-16/bin/pg_ctl -D /opt/postgresql-16/data -l /opt/postgresql-16/logfile start
ExecStop=/opt/postgresql-16/bin/pg_ctl -D /opt/postgresql-16/data -l /opt/postgresql-16/logfile stop
ExecReload=/opt/postgresql-16/bin/pg_ctl -D /opt/postgresql-16/data -l /opt/postgresql-16/logfile restart
[Install]
WantedBy=multi-user.targetEnable and start the service:
sudo systemctl enable postgresql-16
sudo systemctl start postgresql-16Secure your installation by editing pg_hba.conf and postgresql.conf files located in /opt/postgresql-16/data. For performance, consider tuning parameters like shared_buffers, work_mem, and effective_cache_size.
5. Verifying the installation and initial setup
To verify that PostgreSQL 16 is running correctly, switch to the postgres user and connect:
sudo -i -u postgres
/opt/postgresql-16/bin/psql -c "SELECT version();"You should see the PostgreSQL 16 version information, confirming a successful installation.
6. Troubleshooting common installation and configuration issues
If you encounter issues, check the logs located in /opt/postgresql-16/logfile. Common problems include missing dependencies, permission errors, or incorrect configuration settings. Ensure your system packages are up-to-date, and verify that the PostgreSQL user has proper permissions.
Conclusion
Installing PostgreSQL 16 on Ubuntu 24.04 from source gives you full control over your database environment, allowing for custom optimizations and configurations. While it requires more initial setup compared to pre-built packages, the benefits in performance and flexibility are significant. At ByteHosting, we provide reliable VPS hosting in Frankfurt am Main, perfect for hosting your PostgreSQL instances. Follow this guide, and you'll have a robust PostgreSQL 16 setup tailored to your needs. If you need scalable, high-performance VPS hosting, explore our plans and get started today!