How to Install PostgreSQL on Debian

Install PostgreSQL on Debian 11, 12, or 13 with apt install postgresql, verify systemd and psql, create users and databases, add the PGDG repository for other majors, and remove clusters cleanly with pg_dropcluster.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

How to Install PostgreSQL on Debian

PostgreSQL is the open-source relational database behind many web apps, analytics stacks, and GIS workloads. On Debian, the postgresql metapackage installs the server, creates a local cluster, starts systemd service postgresql, and includes psql for administration.

This guide covers install PostgreSQL on Debian for Debian 11 (Bullseye), 12 (Bookworm), and 13 (Trixie): install from Debian main, verify with systemctl and psql, create roles and databases, add the PostgreSQL Apt Repository (PGDG) for other majors, update, and uninstall. I ran these steps on Debian 13 and kept real terminal output below. For client tools only (no local server), see install psql on Debian.

Tested on: Debian 13 (trixie); kernel 6.12.94+deb13-amd64; amd64; PostgreSQL 17.10; port 5432; data at /var/lib/postgresql/17/main.

NOTE
apt install postgresql installs a running database server on your machine. Use postgresql-client or postgresql-client-N when you only need psql to reach a remote instance—see install psql on Debian.

Choose an install method

Method Best for Installs server?
apt install postgresql Default Debian server matching your release Yes (17 on Trixie)
apt install postgresql-N Pin a specific major from Debian main Yes
PGDG PostgreSQL 18, 16, or latest minors from PostgreSQL.org Yes (per postgresql-N package)
postgresql-client only Connect to RDS, Docker, or remote Postgres No — psql guide

Most readers should start with sudo apt install postgresql when they want a local database on Debian.

Default PostgreSQL major by Debian release

Debian Codename Default server package
13 trixie postgresql-17
12 bookworm postgresql-15
11 bullseye postgresql-13

Confirm on your host:

bash
apt-cache policy postgresql postgresql-17 2>/dev/null | head -12

On Trixie before install:

text
postgresql:
  Candidate: 17+278
postgresql-17:
  Candidate: 17.10-0+deb13u1

Prerequisites

  • Debian 11, 12, or 13 on amd64, arm64, or other PGDG architectures.
  • sudo for package installation.
  • Disk space for the data directory (default cluster uses 128MB shared_buffers and grows with your data).
  • APT mirrors configured.
bash
. /etc/os-release && echo "$PRETTY_NAME ($VERSION_CODENAME)"
command -v psql || echo "psql: not installed"

Install PostgreSQL from Debian apt

Per Debian Wiki — PostgreSQL and postgresql.org Debian download, the supported path on current Debian is postgresql from main:

bash
sudo apt update
sudo apt install -y postgresql

Cluster initialization (excerpt from install output):

text
selecting default time zone ... America/New_York
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Setting up postgresql (17+278) ...

Packages installed on Trixie:

text
ii  postgresql                17+278
ii  postgresql-17             17.10-0+deb13u1
ii  postgresql-client-17      17.10-0+deb13u1
ii  postgresql-client-common  278
ii  postgresql-common         278

Verify the server

Service status

bash
sudo systemctl status postgresql --no-pager | head -10
pg_lsclusters
text
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (...; enabled; preset: enabled)
     Active: active (exited) since Mon 2026-06-29 09:52:21 EDT
Ver Cluster Port Status Owner    Data directory              Log file
17  main    5432 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17-main.log

Version and port

bash
psql --version
sudo -u postgres psql -c "SELECT version();"
sudo -u postgres psql -c "SHOW port;"
sudo -u postgres psql -c "SHOW data_directory;"
text
psql (PostgreSQL) 17.10 (Debian 17.10-0+deb13u1)
 PostgreSQL 17.10 (Debian 17.10-0+deb13u1) on x86_64-pc-linux-gnu, compiled by gcc ...
 port
------
 5432
 data_directory
---------------------------
 /var/lib/postgresql/17/main

List databases:

bash
sudo -u postgres psql -c "\l"
text
postgres  | postgres | UTF8 | libc | en_US.UTF-8 | ...
 template0 | postgres | UTF8 | libc | en_US.UTF-8 | ...
 template1 | postgres | UTF8 | libc | en_US.UTF-8 | ...

Create a role and database

As the postgres system user (peer authentication on local sockets):

bash
sudo -u postgres psql -c "CREATE USER appuser WITH PASSWORD 'your_password';"
sudo -u postgres createdb appdb -O appuser
sudo -u postgres psql -c "\du"
text
Role name |                         Attributes
-----------+------------------------------------------------------------
 appuser   |
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS

Connect to the new database:

bash
sudo -u postgres psql -d appdb -c "SELECT current_database(), current_user;"

From an interactive login shell you can also use createuser and createdb. In non-interactive environments, createuser --interactive may block waiting for a TTY—prefer psql -c "CREATE USER ..." in scripts.


Install another major from PGDG

When you need PostgreSQL 18, an older 15 on Trixie, or packages ahead of Debian security, add PGDG.

bash
sudo apt install -y postgresql-common curl ca-certificates
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
apt-cache policy postgresql-17 | head -10

Manual one-line alternative:

bash
sudo apt install -y curl ca-certificates gnupg
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
  | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] https://apt.postgresql.org/pub/repos/apt \
  $(. /etc/os-release && echo "$VERSION_CODENAME")-pgdg main" \
  | sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update

After adding PGDG on Trixie:

text
Get:8 https://apt.postgresql.org/pub/repos/apt trixie-pgdg InRelease
postgresql-17:
  Candidate: 17.10-1.pgdg13+1
 *** 17.10-0+deb13u1  (installed from Debian security)

Install a specific major (example 18):

bash
sudo apt install -y postgresql-18
pg_lsclusters

Multiple majors can coexist—each gets its own port if configured. Full PGDG client/server matrix is documented in install psql on Debian.

IMPORTANT
PGDG is maintained by the PostgreSQL Global Development Group, not the Debian project—review your policy before adding apt.postgresql.org on production systems.

Connect with psql

Local socket (as postgres):

bash
sudo -u postgres psql

TCP to localhost:

bash
psql -h 127.0.0.1 -U postgres -d postgres

Remote server:

bash
psql -h db.example.com -p 5432 -U appuser -d appdb

For connection flags, SSL, and pg_dump, see install psql on Debian.


Update PostgreSQL

bash
sudo apt update
sudo apt install --only-upgrade postgresql postgresql-17
sudo systemctl restart postgresql
psql --version

Major upgrades between PostgreSQL versions require pg_upgradecluster or dump/restore—apt upgrade applies minor/security updates within the same major. Read Debian Wiki — PostgreSQL before jumping majors on production data.


Uninstall PostgreSQL

Remove Debian default cluster

bash
sudo systemctl stop postgresql
sudo pg_dropcluster 17 main --stop
sudo apt purge postgresql-17 postgresql-client-17 postgresql
sudo apt autoremove --purge

Dry-run removing only the metapackage:

text
REMOVING:
  postgresql
Remv postgresql [17+278]

That removes the metapackage only—postgresql-17 and data remain until you purge versioned packages and drop clusters.

Remove PGDG repository

bash
sudo rm -f /etc/apt/sources.list.d/pgdg.list /etc/apt/sources.list.d/pgdg.sources
sudo apt update

Troubleshooting

Symptom Likely cause Fix
psql: command not found Server not installed sudo apt install postgresql or client guide
Connection refused on 127.0.0.1 Server not running or wrong port pg_lsclusters; sudo systemctl start postgresql
peer authentication failed Wrong OS user for socket auth Use sudo -u postgres psql or configure pg_hba.conf
createuser hangs in scripts Interactive password prompt Use psql -c "CREATE USER ..."
Two versions after PGDG Multiple clusters pg_lsclusters; connect with psql -p PORT
apt install postgresql pulls wrong major Suite default Install explicit postgresql-N or use PGDG

References


Summary

Install PostgreSQL on Debian with sudo apt install postgresql to get the suite default server (17.10 on Trixie), systemd service postgresql, and psql. Confirm with pg_lsclusters, SHOW port, and SELECT version(). Create roles and databases as postgres, add PGDG when you need another major, and remove clusters with pg_dropcluster before apt purge. For client-only installs, use install psql on Debian.


Frequently Asked Questions

1. How do I install PostgreSQL on Debian?

Run sudo apt update && sudo apt install -y postgresql. Debian creates a local cluster, starts the postgresql service, and installs psql. Verify with sudo systemctl status postgresql and psql --version.

2. Which PostgreSQL version does Debian install by default?

Debian 13 (trixie) ships PostgreSQL 17, Debian 12 (bookworm) PostgreSQL 15, and Debian 11 (bullseye) PostgreSQL 13. The postgresql metapackage tracks the suite default—run apt-cache policy postgresql-17 on your release.

3. What is the difference between postgresql and postgresql-client on Debian?

postgresql installs the database server, data directory, and systemd service. postgresql-client installs psql and backup tools only. For a remote-only client, see install psql on Debian instead of the full server package.

4. How do I install a different PostgreSQL major on Debian?

Add the PostgreSQL Apt Repository (PGDG) from postgresql.org/download/linux/debian, then sudo apt install postgresql-18 or another postgresql-N package. Use pg_lsclusters to see running clusters.

5. How do I create a PostgreSQL user and database on Debian?

As postgres: sudo -u postgres psql -c "CREATE USER myuser WITH PASSWORD 'secret';" and sudo -u postgres createdb mydb -O myuser. Or use createuser and createdb interactively from a login shell.

6. What port does PostgreSQL use on Debian?

Default port 5432. Confirm with sudo -u postgres psql -c "SHOW port;" or pg_lsclusters.

7. Where is the PostgreSQL data directory on Debian?

Typically /var/lib/postgresql/N/main for major version N—for example /var/lib/postgresql/17/main on Debian 13 with PostgreSQL 17. pg_lsclusters lists cluster paths.

8. How do I uninstall PostgreSQL from Debian?

Stop the service, run sudo pg_dropcluster N main --stop, then sudo apt purge postgresql-N postgresql-client-N postgresql-common. Remove PGDG sources if you added them.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …