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_ipA 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 postgresqlThis 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 postgresqlRun 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 5432The 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_ 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 psqlThe 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_ 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_ in postgresql.conf before you set passwords; it is the default from version 14.
Where the Files Are
| Item | Ubuntu and Debian | AlmaLinux and Rocky |
|---|---|---|
| Config | /etc/ | /var/ |
| Data | /var/ | /var/ |
| Service | postgresql | postgresql |
On Ubuntu and Debian, replace 16 with your version; ls /etc/postgresql/ shows it. Both families keep postgresql.conf and pg_ 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 appThe 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_for each database and copy the dumps off the server. A copy of the running data folder is not a consistent backup.dump - Update: minor versions arrive with normal system updates. A new major version needs a planned upgrade of the cluster.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations