Skip to content
Commitpedia
Log in Join & edit

Install PostgreSQL and connect with psql on Debian, Ubuntu and RHEL

Install the PostgreSQL server from your distribution or the official PGDG repositories, start it, and open your first psql session as the postgres user.

Not checked by a trusted editor yet Last edited Outdated?

Edit history of Install PostgreSQL and connect with psql on Debian, Ubuntu and RHEL: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
3 min

PostgreSQL is packaged by every major Linux distribution, and the PostgreSQL project also runs its own repositories (PGDG) with every supported major version. The distribution package is the quickest start; the PGDG repository lets you pick the major version yourself. This guide installs the server, checks that it runs and connects with psql, the command-line client.

Debian and Ubuntu

The distribution package installs whatever version that release ships:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo apt update
sudo apt install postgresql

To choose a major version, enable the PGDG apt repository with the helper script from the postgresql-common package, then install a versioned package. Replace 18 with the version you want:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install postgresql-18

On Debian and Ubuntu the package creates and starts a cluster named main automatically. Configuration lives in /etc/postgresql/<version>/main/ (for example postgresql.conf and pg_hba.conf). Debian’s helper tools show and control clusters:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
pg_lsclusters
sudo pg_ctlcluster 18 main status
sudo systemctl status postgresql

RHEL, Rocky Linux, AlmaLinux and Fedora

Unlike Debian, the Red Hat family does not initialize or start the database for you. With the distribution package:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo dnf install postgresql-server
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql

With the PGDG repository you install the repository RPM first. The download page has a selector that prints the exact commands for your distribution, architecture and version; for PGDG packages they follow this pattern (shown for version 17 on an EL 9 x86_64 system):

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
sudo dnf install -y postgresql17-server
sudo /usr/pgsql-17/bin/postgresql-17-setup initdb
sudo systemctl enable --now postgresql-17

Use the commands the selector prints for your system rather than copying version numbers blindly. Note that the service name includes the version (postgresql-17) for PGDG packages.

Connect for the first time

Installation creates an operating system user and a database superuser, both named postgres. Local connections use peer authentication by default on Debian and Ubuntu, which means the operating system user name must match the database user name. So switch to the postgres OS user to connect:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo -u postgres psql

You get a prompt like postgres=#. The # means you are a superuser. Try a few commands, then quit with \q:

SELECT version();
\conninfo
\l
\q

Create an application user and database

Do not let applications connect as postgres. Create a normal login role and a database it owns. createuser --pwprompt asks for the password, so it does not end up in your shell history:

Check before you run this

  • Runs with administrator rights. Make sure you understand every line.
sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb -O appuser appdb
psql -h localhost -U appuser -d appdb

The last command connects over TCP to localhost, which uses password authentication (normally scram-sha-256 in recent default configurations) instead of peer authentication.

Allowing remote connections

By default the server only listens on the loopback interface (listen_addresses = 'localhost'). To accept connections from other machines you need two changes:

  1. In postgresql.conf, set listen_addresses to the server’s address or to '*' for all interfaces. This setting needs a restart.
  2. In pg_hba.conf, add a rule that allows the client network, for example host appdb appuser 10.0.0.0/24 scram-sha-256. This needs only a reload.
  3. Restart the service, for example sudo systemctl restart postgresql (Debian/Ubuntu) or sudo systemctl restart postgresql-17 (PGDG on RHEL).
  4. Open TCP port 5432 in the host firewall for the client network only.
# postgresql.conf
listen_addresses = '*'
port = 5432

Do not expose port 5432 to the whole internet. Limit both the firewall and the pg_hba.conf rule to the networks that really need access.

Check that the server answers

pg_isready tests whether the server accepts connections without logging in. It exits with 0 when connections are accepted, 1 when the server rejects them (for example during startup), 2 when there is no response and 3 when no attempt was made.

pg_isready -h localhost -p 5432

Official documentation

See also

  • 0199 psql meta-commands cheat sheet The backslash commands you use every day in psql: connecting, listing databases, tables and roles, describing objects, timing queries, expanded output and… Beginner · Reference · Any system Article Install & connect · Roles & administration 15 Oct 2026
  • 0201 pg_hba.conf and PostgreSQL authentication methods pg_hba.conf decides who may connect from where and how they must prove who they are. Rules are checked top to bottom and… Intermediate · Concept · Linux Article Install & connect · Security & access 15 Oct 2026
  • 0210 Fix: FATAL: no pg_hba.conf entry for host The server received the connection but no pg_hba.conf rule matches this client address, user, database and SSL mode. Add or correct a… Intermediate · Fix · Linux Article Install & connect · Security & access 15 Oct 2026
  • 0211 Fix: psql connection to server on socket /var/run/postgresql/.s.PGSQL.5432 failed psql tried the local Unix socket and found nothing there. The server is not running, listens on another port, or uses a… Beginner · Fix · Linux Article Install & connect 15 Oct 2026
  • 0257 Run PostgreSQL properly on a Linux server: install, lock down, back up, monitor and patch Set up PostgreSQL 18 from the official repository for one application: a dedicated role and database, SCRAM logins from one app server… Intermediate · Tutorial · Linux Article Backup & restore · Install & connect · Roles & administration 15 Oct 2026

Repos in the wild

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Commitpedia +4,577 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?