Install PostgreSQL on Linux

This guide covers installing PostgreSQL on common Linux distributions using their native package managers.

Prerequisites

  • A supported Linux distribution (examples below use Debian/Ubuntu, Fedora, and Arch)
  • sudo access for package installation and service management

Debian / Ubuntu (APT)

1. Update package lists


                sudo apt update
            

2. Install PostgreSQL


                sudo apt install -y postgresql postgresql-contrib
            

3. Verify the service

On Debian/Ubuntu, the service is typically postgresql:


                sudo systemctl status postgresql
            

4. Check version


                psql --version
            

5. Connect as the default superuser

The default cluster often creates a system user postgres and a database role named postgres. To open a shell as that user:


                sudo -u postgres psql
            

Or:


                sudo -u postgres createuser --interactive
sudo -u postgres createdb mydb
            

Paths for data and logs vary; on Ubuntu see /etc/postgresql/<version>/main/ for configuration.

Fedora / RHEL-style (DNF)

1. Install packages


                sudo dnf install -y postgresql-server postgresql-contrib
            

2. Initialize and enable the database (first install)


                sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql
            

3. Verify


                sudo systemctl status postgresql
psql --version
            

Switch to the postgres user for admin tasks:


                sudo -u postgres psql
            

Arch Linux (pacman)

1. Install


                sudo pacman -S postgresql
            

2. Initialize the cluster (first time)


                sudo -u postgres initdb -D /var/lib/postgres/data
            

3. Start and enable


                sudo systemctl enable --now postgresql
            

4. Verify


                psql --version
sudo -u postgres psql
            

Post-installation: PATH and psql

If psql is not on your PATH, either log out and back in or add the directory shown by your package manager (often /usr/bin is already correct).

Test a connection:


                psql postgres
            

Troubleshooting

Port 5432 already in use


                sudo ss -tlnp | grep 5432
            

Stop the conflicting service or change port in postgresql.conf.

Permission denied connecting as your user

Create a database role matching your Linux username, or use sudo -u postgres psql and create roles/databases as needed.

Next steps