Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Keep the Database Local

A fresh PostgreSQL server listens on 127.0.0.1 and port 5432 only. Leave it there. An application on the same server connects through localhost or the local socket, as explained in what is localhost. To work on the database from your own computer, open an SSH tunnel instead of a database port:

ssh -L 5433:127.0.0.1:5432 user@your_server_ip

A client on your computer then connects to 127.0.0.1 port 5433. SSH port forwarding covers the details. Only when another server must connect do you open anything, and then for that one address; see how to allow remote connections to PostgreSQL.

Install on Ubuntu and Debian

sudo apt update
sudo apt install postgresql

This installs the server, the psql client, and the cluster tools from the distribution's own repositories. The package creates a cluster named main and starts it. Each release ships one major version: 16 on Ubuntu 24.04, 18 on Ubuntu 26.04, 15 on Debian 12, and 17 on Debian 13. apt policy postgresql shows yours before you install. You need a user with sudo rights; see how to create a sudo user on Ubuntu.

Install on AlmaLinux and Rocky Linux

On the RHEL family the package does not create a database cluster. Initialize it once, then start the service:

sudo dnf install postgresql-server
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql

Run dnf info postgresql-server to see which version the repositories offer. Release 9 installs PostgreSQL 13 by default, which is older than every version the PostgreSQL project still supports; newer versions are module streams, listed by dnf module list postgresql. Enable one before the first install, for example sudo dnf module enable postgresql:16. Release 10 has no modules: postgresql-server is version 16, and a newer version comes as a package of its own, such as postgresql18-server. Choose before the first install, because moving a cluster to a new major version later needs an upgrade of the data.

Check the Service and the Version

sudo systemctl status postgresql
psql --version
sudo ss -tlnp | grep 5432

The last line should show 127.0.0.1 (and ::1) for port 5432. On Ubuntu and Debian, postgresql is an umbrella service that shows "active (exited)"; each cluster runs as a unit of its own, and pg_lsclusters lists each cluster with its version, port, and status. If a cluster does not start, read the messages of all PostgreSQL units with sudo journalctl -u 'postgresql*', and on Ubuntu and Debian also the files in /var/log/postgresql/. The journalctl guide shows how to filter them.

The postgres User and psql

The installation creates a Linux user named postgres and a database superuser of the same name. Local logins use peer authentication: PostgreSQL checks that the Linux user running the client has the same name as the database role. That is why no password is asked here:

sudo -u postgres psql

The prompt changes to postgres=#. Type \q to leave. Keep the postgres role for administration only and give it no password; with peer authentication only users with sudo rights get in.

TCP connections to 127.0.0.1 differ by family. On Ubuntu and Debian, pg_hba.conf sends them to scram-sha-256 password checks. On AlmaLinux and Rocky Linux, postgresql-setup --initdb writes ident for them, so a password login over TCP fails until you change those two host lines (127.0.0.1 and ::1) to scram-sha-256 and reload the service. On PostgreSQL 13, also set password_encryption = 'scram-sha-256' in postgresql.conf before you set passwords; it is the default from version 14.

Where the Files Are

ItemUbuntu and DebianAlmaLinux and Rocky
Config/etc/postgresql/16/main//var/lib/pgsql/data/
Data/var/lib/postgresql/16/main//var/lib/pgsql/data/
Servicepostgresqlpostgresql

On Ubuntu and Debian, replace 16 with your version; ls /etc/postgresql/ shows it. Both families keep postgresql.conf and pg_hba.conf in the configuration folder.

Create a First Database

Create a role for your application and a database it owns:

sudo -u postgres createuser --pwprompt app_user
sudo -u postgres createdb --owner=app_user app

The first command asks for the new password twice, so it never lands in your shell history. How to create a user in PostgreSQL explains roles and privileges in more detail.

Next Steps

  • Learn the client: the psql commands cheat sheet lists the commands you use every day.
  • Back up: schedule pg_dump for each database and copy the dumps off the server. A copy of the running data folder is not a consistent backup.
  • Update: minor versions arrive with normal system updates. A new major version needs a planned upgrade of the cluster.
Was this answer helpful?
Loading...