Learn how to set up the latest PostgreSQL 18 on an Ubuntu 24.04 VPS. This step-by-step guide covers installation, creating users via command line, and configuring secure remote access for your development workflow.

By Hardik Kanajariya
As a full-stack developer, managing your own database infrastructure gives you complete control over performance and security. With the release of PostgreSQL 18, we get even better performance and security features. In this guide, I'll walk you through setting up PostgreSQL 18 on an Ubuntu 24.04 VPS, securing it, and enabling remote access for your development tools.
Ubuntu's default repositories often lag behind the latest major versions. To get PostgreSQL 18, we need to add the official PostgreSQL Apt Repository.
First, update your system and install the necessary tools:
sudo apt update && sudo apt install ca-certificates gnupg lsb-release
Next, add the PostgreSQL GPG key and repository:
# Add GPG Key
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
# Add Repository
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
Now that the repository is added, update your package list and install the server and client:
sudo apt update
sudo apt install -y postgresql-18 postgresql-client-18
Ensure that the database service is running and set to start automatically on reboot:
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql
You should see Active: active (running) in the output.
Instead of switching to the interactive shell, you can use this efficient one-liner to create a user and database simultaneously. This is great for automation scripts.
Replace my_user, secure_password, and my_db with your own credentials:
sudo -u postgres psql -c "CREATE USER my_user WITH PASSWORD 'secure_password';"
sudo -u postgres psql -c "CREATE DATABASE my_db OWNER my_user;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE my_db TO my_user;"
By default, Postgres only listens on localhost. If you need to connect from a tool like TablePlus or pgAdmin on your local machine, or from a Docker container, follow these steps.
postgresql.confEdit the main configuration file:
sudo nano /etc/postgresql/18/main/postgresql.conf
Find the line listen_addresses = 'localhost' and change it to:
listen_addresses = '*'
pg_hba.confNow, tell Postgres which IP addresses are allowed to authenticate.
sudo nano /etc/postgresql/18/main/pg_hba.conf
Add this line to the end of the file to allow password authentication via scram-sha-256 (the secure standard):
host all all 0.0.0.0/0 scram-sha-256
Apply the changes:
sudo systemctl restart postgresql
Opening your database to 0.0.0.0/0 (the whole internet) is risky. Use UFW (Uncomplicated Firewall) to restrict access to only your IP address.
# Allow SSH first (don't lock yourself out!)
sudo ufw allow ssh
# Allow Postgres ONLY from your specific IP (e.g., 203.0.113.1)
sudo ufw allow from 203.0.113.1 to any port 5432
# Enable the firewall
sudo ufw enable
Note: If you are connecting from a Docker container on the same server, you generally don't need to open port 5432 externally if you use the Docker network gateway IP.
You now have a production-ready PostgreSQL 18 server running on Ubuntu 24.04. You've secured it with a custom user, configured remote access, and locked down the firewall.
Happy Coding!
Get the latest articles, tutorials, and updates delivered straight to your inbox. No spam, unsubscribe at any time.