Add the PGDG repository
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail 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 PostgreSQL 17
sudo apt install -y postgresql-17 postgresql-client-17
sudo systemctl enable postgresql
sudo systemctl start postgresql
Verify the version:
psql --version
# psql (PostgreSQL) 17.7 (Ubuntu 17.7-3.pgdg24.04+1)
Set password and create app user
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'CHANGE_ME';"
sudo -u postgres psql -c "CREATE USER prod_user WITH PASSWORD 'CHANGE_ME';"
sudo -u postgres psql -c "CREATE DATABASE prod_db OWNER prod_user;"
Grant ownership/full privileges explicitly if Prisma migrations fail with permission errors:
sudo -u postgres psql -d prod_db -c "GRANT ALL PRIVILEGES ON DATABASE prod_db TO prod_user;"
sudo -u postgres psql -d prod_db -c "ALTER DATABASE prod_db OWNER TO prod_user;"
Remote access (optional)
Only do this if you need TCP access from outside localhost (e.g. Docker bridge, remote tooling):sudo sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/17/main/postgresql.conf
echo "host all all 0.0.0.0/0 scram-sha-256" | sudo tee -a /etc/postgresql/17/main/pg_hba.conf
sudo systemctl restart postgresql
Don’t forget the port flag if connecting via Docker:
-p 5432:5432. Missing it is a common cause of “connection refused” errors.Useful checks
sudo systemctl status postgresql --no-pager
sudo -u postgres psql -c "SELECT version();"
ls /usr/lib/postgresql/ # list installed major versions