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. The examples use Ubuntu 26.04 LTS with PostgreSQL 18; run the commands as root (connect over SSH).
If your app asks for MySQL or MariaDB instead, see MariaDB on Your Linux Server: Install, Create a Database and a User.
1. Install PostgreSQL
apt install postgresql
The database starts at once and after every restart of the server. Check it:
pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file 18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log
online means that it runs. It 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 4 shows how you still reach it from your computer.
2. 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.
3. 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, over the network address of the server itself:
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.
4. 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 3. More about such tunnels: Open a Web Page That Listens Only on Your Server: SSH Tunnel.
5. 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
Run the backup every night with cron (Run Tasks on a Schedule on Linux: cron and systemd Timers) and keep a copy away from the server (Automatic Encrypted Backups of Your Server to Another Server With restic). A backup on the same disk does not help when the disk fails.
If Something Does Not Work
- Peer authentication failed for user "appuser": you left out
-h 127.0.0.1. Without it, PostgreSQL does not ask for the password and only lets in a system user with the same name. Use the command from step 3. - password authentication failed for user "appuser": the password is wrong. Set a new one as administrator:
sudo -u postgres psql, then\password appuser. - The database does not run:
systemctl status postgresqland the log file named bypg_lsclusterstell you why. More: A Service Does Not Start: Find Out Why.
Good to Know
- The settings are in
/etc/postgresql/18/main/:postgresql.conffor memory and connections,pg_hba.conffor who may log in from where. After a change, runsystemctl restart postgresql. - Delete a database or a user you no longer need:
sudo -u postgres dropdb appdb_copyandsudo -u postgres dropuser appuser. - Disk space: the data is in
/var/lib/postgresql. See What Fills the Disk and What Loads the Server.