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
sudorights. If you are logged in asroot, leave outsudo(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
- Ubuntu / Debian
- AlmaLinux / Rocky
1
Install the package
2
Check that it is running
pg_lsclusters should show your cluster as online.How to log in and create a user and database
PostgreSQL creates a Linux user and a database superuser, both calledpostgres. 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
postgres=#.2
Create a login role
3
Create a database owned by it
4
Leave psql
Type
\q and press Enter.How to check it worked
Connect as the new user over TCP on the server itself: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.How to back up a PostgreSQL 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 ranpsql -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?
Runsudo -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.Related guides
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:- Live Chat: Quick assistance via our website.
- Support Ticket: Open a Ticket
- Discord: Join our Community
- Email: support@freakhosting.com
Last Updated: November 2026