Skip to main content
To install PostgreSQL on Ubuntu or Debian, run sudo apt update and sudo apt install postgresql. The service starts by itself. Open the database shell with sudo -u postgres psql, then create a login with CREATE ROLE alex WITH LOGIN PASSWORD '…'; and a database with CREATE DATABASE appdb OWNER alex;. On AlmaLinux or Rocky, install postgresql-server and run sudo postgresql-setup --initdb first.

Before you start

  • You need a user with sudo rights. If you are logged in as root, leave out sudo (but keep -u postgres).
  • Update your packages first.
  • PostgreSQL is a database server, like MySQL, but with its own commands and tools. Use it if your app asks for PostgreSQL (for example many Django, Rails and Nextcloud setups).

Which PostgreSQL version will I get?

Sources: Debian 13 release notes, Red Hat PostgreSQL guide for RHEL 9 and for RHEL 10.

How to install PostgreSQL

1

Install the package

2

Check that it is running

pg_lsclusters should show your cluster as online.
Need a newer major version? The PostgreSQL project’s own repository has it. Follow the official Ubuntu instructions:

How to log in and create a user and database

PostgreSQL creates a Linux user and a database superuser, both called postgres. Locally it uses peer authentication: your Linux user name must match the database role, so you log in by switching to postgres.
1

Open psql

The prompt changes to postgres=#.
2

Create a login role

3

Create a database owned by it

The owner has full rights on the database.
4

Leave psql

Type \q and press Enter.

How to check it worked

Connect as the new user over TCP on the server itself:
If you see Ident authentication failed (possible on AlmaLinux or Rocky), open pg_hba.conf, set the last column of the host lines for 127.0.0.1/32 and ::1/128 to scram-sha-256, and restart PostgreSQL.

Where are the PostgreSQL config files?

Replace 16 with your version. The default port is 5432, and the server only listens on localhost after installation.

How to allow remote PostgreSQL connections

Only do this if another server must connect. For your own admin tools, use an SSH tunnel instead.
1

Listen on the network

In postgresql.conf, set:
2

Allow one client in pg_hba.conf

Add a line at the end, using the client’s IP:
3

Restart PostgreSQL

4

Open port 5432 to that IP only

UFW: sudo ufw allow from 198.51.100.25 to any port 5432 proto tcp. For firewalld, use a rich rule as in set up firewalld.
Never add a pg_hba.conf line with 0.0.0.0/0 and trust. trust lets anyone log in without a password. Always use scram-sha-256 and the narrowest IP range possible.

How to back up a PostgreSQL database

Restore it into an empty database:
-Fc writes PostgreSQL’s compressed custom format, which pg_restore reads. For every database and all roles, use sudo -u postgres pg_dumpall > all.sql.

Frequently asked questions

Why does psql say “Peer authentication failed for user”?

You ran psql -U alex as a different Linux user. Add -h 127.0.0.1 to log in with a password over TCP, or switch to the matching Linux user.

How do I set a password for the postgres user?

Run sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'X-Strong-Pass1';". You only need it if a tool must log in as postgres over TCP.

Can PostgreSQL and MySQL run on the same server?

Yes. They use different ports (5432 and 3306) and do not conflict.

Which is better, PostgreSQL or MySQL?

Use the one your app supports. WordPress, for example, needs MySQL or MariaDB. If your app supports both, pick the one you already know how to run and back up.

VPS and dedicated server guides

Every guide in this section.

Install MariaDB or MySQL

The other common database server.

Install Redis

Add a fast cache next to PostgreSQL.

Deploy a Python app

Run an app that uses PostgreSQL.

Set up UFW

Keep port 5432 closed to the internet.

Checked against the official Ubuntu Server PostgreSQL guide, PostgreSQL download page and Red Hat PostgreSQL guide on 4 November 2026; install, role, database, pg_dump and pg_restore commands tested on Ubuntu 24.04 LTS with PostgreSQL 16. Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026