Many apps keep their data in a PostgreSQL database. This page installs PostgreSQL, creates a database with its own user for your app, shows how to reach it safely from your computer and how to back it up. On AlmaLinux and Rocky Linux three things differ from other systems: you create the database files yourself, you start the service yourself, and logins with a password are switched off until you allow them. The examples use AlmaLinux 10 (PostgreSQL 16) and Rocky Linux 9 (PostgreSQL 13); run the commands as root.
If your app asks for MySQL or MariaDB instead, see MariaDB on AlmaLinux and Rocky Linux: Install, Create a Database and a User.
1. Install and Start PostgreSQL
dnf install postgresql-server postgresql-setup --initdb systemctl enable --now postgresql
- The second command creates the database files in
/var/lib/pgsql/data. Without it the service does not start. - The third command starts PostgreSQL now and after every restart of the server.
Check it:
systemctl is-active postgresql
active
PostgreSQL listens on port 5432 and only on the server itself (127.0.0.1), so nobody can reach it from the Internet. Keep it that way; step 5 shows how you still reach it from your computer.
2. Allow Logins With a Password
At first, a program on the server can log in to the database only as the system user with the same name. Apps log in with a username and a password instead, so allow that for connections from the server itself. Run these three commands once:
echo "password_encryption = scram-sha-256" >> /var/lib/pgsql/data/postgresql.conf sed -i -E 's/^(host\s+all\s+all\s+(127\.0\.0\.1\/32|::1\/128)\s+)ident$/\1scram-sha-256/' /var/lib/pgsql/data/pg_hba.conf systemctl restart postgresql
The first line makes PostgreSQL store passwords in the current, safe form. The second changes the word ident to scram-sha-256 in the two lines of pg_hba.conf that are about connections from the server itself. Do this before you create users in step 4.
3. Log In as Administrator
The administrator of the database is the system user postgres. It has no password; you become this user with sudo:
sudo -u postgres psql
The prompt changes to postgres=#. Three commands are enough to look around: \l lists the databases, \du lists the users, \q leaves.
4. A Database and a User for Your App
Give every app its own database and its own user, so that one app cannot read the data of another. Create the user first. The command asks twice for a password for it; choose a long one:
sudo -u postgres createuser --pwprompt appuser
Enter password for new role: Enter it again:
Then create the database that belongs to this user:
sudo -u postgres createdb --owner=appuser appdb
Test the login the way your app will do it:
psql -h 127.0.0.1 -U appuser appdb
Enter the password. The prompt appdb=> means that it works; leave with \q. In the settings of your app, enter:
| Setting | Value |
|---|---|
| Host | 127.0.0.1 |
| Port | 5432 |
| Database | appdb |
| User | appuser |
| Password | the password you chose |
Some apps want all of this in one line: postgresql://appuser:PASSWORD@127.0.0.1:5432/appdb.
5. Open the Database From Your Computer
To work with the database in a program on your computer, such as pgAdmin or DBeaver, do not open port 5432 to the Internet. Send it through SSH instead. Run this on your computer and leave the window open:
ssh -L 5432:127.0.0.1:5432 root@your-server
Now the program on your computer connects to host 127.0.0.1, port 5432, with the user and password from step 4. More about such tunnels: Open a Web Page That Listens Only on Your Server: SSH Tunnel.
6. Back Up and Restore
Save one database into a compressed file:
sudo -u postgres pg_dump appdb | gzip > /root/appdb-$(date +%F).sql.gz
Save all databases and users at once:
sudo -u postgres pg_dumpall | gzip > /root/all-databases-$(date +%F).sql.gz
To restore a database, create an empty one and load the file into it:
sudo -u postgres createdb --owner=appuser appdb_copy gunzip -c /root/appdb-2026-12-02.sql.gz | sudo -u postgres psql appdb_copy
Keep a copy of the backup away from the server. A backup on the same disk does not help when the disk fails.
If Something Does Not Work
- The service does not start after the installation and the message ends with PostgreSQL database server: the database files do not exist yet. Run
postgresql-setup --initdb(step 1) and start the service again. - Ident authentication failed for user "appuser": logins with a password are not allowed yet. Do step 2.
- password authentication failed for user "appuser": the password is wrong, or the user was created before step 2. Set the password again as administrator:
sudo -u postgres psql, then\password appuser. - Peer authentication failed for user "appuser": you left out
-h 127.0.0.1. Use the command from step 4. - could not change directory to "/root": Permission denied: a harmless note that appears on version 9 when you run a command as
postgresfrom the folder/root. The command still works; to avoid the note, runcd /tmpfirst. - The service does not run for another reason: A Service Does Not Start: Find Out Why. The log files of PostgreSQL are in
/var/lib/pgsql/data/log.
Good to Know
- The settings are in
/var/lib/pgsql/data:postgresql.conffor memory and connections,pg_hba.conffor who may log in from where. After a change, runsystemctl restart postgresql. - The version depends on the system: AlmaLinux and Rocky Linux 9 install PostgreSQL 13, version 10 installs PostgreSQL 16.
- Delete a database or a user you no longer need:
sudo -u postgres dropdb appdb_copyandsudo -u postgres dropuser appuser. - The firewall needs no change: the database is used only on the server itself. See Protect Your AlmaLinux or Rocky Linux Server With firewalld and Fail2ban.