Install Postgres on Ubuntu with sudo apt install postgresql (gets Ubuntu’s default version) or via PGDG repo (gets latest Postgres). The PGDG path is the right choice for production where you need a specific version. The team that uses Ubuntu’s default has a quick install for dev. The team that uses PGDG has the latest features and security patches.
Table of contents
- The apt install path
- The PGDG repo path (for latest version)
- Verify the install
- Connect to the default postgres user
- Configure for remote connections
- Install pgAdmin (optional GUI)
- Post-install security
- FAQ
The apt install path
sudo apt update
sudo apt install postgresql postgresql-contrib
This installs Ubuntu’s default Postgres version. On Ubuntu 24.04 it’s Postgres 16. On 22.04 it’s 14. The team that uses this has a working Postgres in 30 seconds.
The PGDG repo path (for latest version)
Add the PostgreSQL official repo:
# Add PGDG repo and GPG key
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt update
# Install latest
sudo apt install postgresql-18
The team that uses PGDG has Postgres 18 (or whatever version they specify) on Ubuntu 22.04 or 24.04.
Verify the install
psql --version
sudo systemctl status postgresql
The team that checks both confirms the install and that the service is running.
Connect to the default postgres user
Ubuntu’s install creates a postgres system user. Switch to it:
sudo -i -u postgres
psql
Now you’re in psql as the postgres superuser. The team that uses peer authentication (Ubuntu default) skips password auth entirely for local connections.
Configure for remote connections
By default, Postgres listens on localhost only. To allow remote:
# /etc/postgresql/16/main/postgresql.conf
listen_addresses = '*'
# /etc/postgresql/16/main/pg_hba.conf
host all all 0.0.0.0/0 md5
Then sudo systemctl restart postgresql. The team that opens remote access also opens the port in the firewall (5432).
Install pgAdmin (optional GUI)
# Add pgAdmin repo
curl -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://apt.pgadmin.org/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'
sudo apt update
sudo apt install pgadmin4
Access at http://localhost/pgadmin4 (after sudo /usr/pgadmin4/bin/setup-web.sh). The team that uses pgAdmin has a GUI for occasional tasks.
Post-install security
- Change
postgresuser password:sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'newpwd';". - Don’t expose port 5432 publicly unless needed (use VPN or SSH tunnel).
- Configure
pg_hba.confto require md5 or scram-sha-256 for remote. - Enable connection logging in
postgresql.conf. - Regular backups (pg_dump, pg_basebackup, or managed).
FAQ
What’s the latest Postgres version?
PostgreSQL 18 was released in 2025. PGDG has 18 available. The team that needs the latest features uses PGDG.
Can I run multiple Postgres versions on Ubuntu?
Yes - PGDG supports installing 14, 15, 16, 17, 18 side-by-side. Each gets its own port. The team that runs migrations across versions uses this.
Where are the config files?
/etc/postgresql/<version>/main/postgresql.conf and /etc/postgresql/<version>/main/pg_hba.conf. Edit then sudo systemctl reload postgresql.
How do I create a database?
sudo -u postgres createdb mydb or in psql: CREATE DATABASE mydb;. The team that uses the CLI has scriptable setup.
Where do Postgres logs go?
/var/log/postgresql/postgresql-<version>-main.log. Configure log_directory, log_filename, logging_collector in postgresql.conf.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: