On this page
- Distro package or PGDG repository: which should you use?
- How do you install PostgreSQL on Ubuntu or Debian?
- How do you install PostgreSQL on Rocky Linux or AlmaLinux?
- How do you create a role and a database?
- How do pg_hba.conf rules work?
- Where should PostgreSQL listen?
- Which settings are worth changing first?
- How do you verify, back up and upgrade PostgreSQL?
- Questions
Distro package or PGDG repository: which should you use?
Use the distro package when its major version is new enough for your application, and PGDG when you want the current major release or the same version across several servers. Ubuntu 24.04 ships PostgreSQL 16, Ubuntu 22.04 ships 14, Debian 12 ships 15 and Debian 13 ships 17. Rocky Linux 9 and AlmaLinux 9 default to 13 and offer newer module streams.
PGDG is run by the PostgreSQL project itself and publishes every supported major version for each distribution, along with extensions such as PostGIS built for those versions. Each major release gets five years of fixes, so either route gives you a supported server today. Whichever you pick, stay with one source per server.
A modest plan handles a surprising amount: PostgreSQL itself idles in under 100 MB, and the real driver of memory needs is how much of your data you want cached. Start from your normal sudo user as described in the first SSH session tutorial.
How do you install PostgreSQL on Ubuntu or Debian?
Install postgresql for the distro version, or run the PGDG helper script that ships in postgresql-common and then install a specific major version.
Distro version
sudo apt update
sudo apt install -y postgresql
psql --versionPGDG repository
sudo apt update
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt install -y postgresql-18
psql --version
pg_lsclustersThe Debian packaging creates a cluster called main, starts it and keeps its config under /etc/postgresql/18/main/ with data in /var/lib/postgresql/18/main/. pg_lsclusters lists every cluster with its port and status, which is handy when two major versions sit side by side during an upgrade.
How do you install PostgreSQL on Rocky Linux or AlmaLinux?
Install the PGDG repository RPM, disable the built-in module so the two sources do not clash, then install the server package and initialise the data directory.
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
sudo dnf install -y postgresql18-server
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
sudo systemctl enable --now postgresql-18
/usr/pgsql-18/bin/psql --versionConfig and data live together in /var/lib/pgsql/18/data/. If you prefer the AppStream build, pick a stream instead:
sudo dnf module list postgresql
sudo dnf module enable -y postgresql:16
sudo dnf install -y postgresql-server
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresqlThat variant keeps its files in /var/lib/pgsql/data/. Unlike Debian, nothing starts until you run the init step and enable the unit.
How do you create a role and a database?
Switch to the postgres system user, create a login role with a password, then create a database owned by that role.
sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb --owner=appuser appdb
sudo -u postgres psql -c "\du"The same thing in SQL, which is easier to keep in a setup script:
sudo -u postgres psql
CREATE ROLE appuser LOGIN PASSWORD 'Use-A-Long-Random-Secret';
CREATE DATABASE appdb OWNER appuser;
\qSince PostgreSQL 15 ordinary roles can no longer create objects in the public schema of a database they do not own. Making the application role the owner of its database, as above, avoids that surprise. Read-only reporting roles and default privileges are covered in PostgreSQL roles and permissions.
How do pg_hba.conf rules work?
Each line in pg_hba.conf says which connection type, database, role and source address may connect and how it must authenticate; PostgreSQL uses the first line that matches.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host appdb appuser 127.0.0.1/32 scram-sha-256
host appdb appuser ::1/128 scram-sha-256peer trusts the Linux user name on the local socket, which is why sudo -u postgres psql works without a password. scram-sha-256 is the modern password method and the default for new passwords. On Rocky and Alma, check the host lines after initdb: some builds write ident there, which rejects password logins from applications until you change it.
Apply changes without a restart:
sudo -u postgres psql -c "SELECT pg_reload_conf();"
sudo -u postgres psql -c "SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules;"The second query shows how the server parsed the file, including an error column if a line is broken.
Where should PostgreSQL listen?
Leave listen_addresses at localhost unless another machine truly needs a direct connection. Apps on the same server connect over 127.0.0.1 or the Unix socket, and remote administration works through an SSH tunnel.
sudo -u postgres psql -c "SHOW listen_addresses;"
sudo ss -ltnp | grep 5432If a second server must connect, set the address in postgresql.conf and allow only that source in both pg_hba.conf and the firewall:
# postgresql.conf
listen_addresses = 'localhost,198.51.100.24'
# pg_hba.conf
hostssl appdb appuser 203.0.113.50/32 scram-sha-256sudo ufw allow from 203.0.113.50 to any port 5432 proto tcp # Ubuntu, Debian
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" port port="5432" protocol="tcp" accept'
sudo firewall-cmd --reload # Rocky, AlmaChanging listen_addresses needs a full restart: sudo systemctl restart postgresql (or postgresql-18 on Rocky and Alma). The hostssl type forces TLS; Debian and Ubuntu enable a snakeoil certificate by default, and on other systems you set ssl = on with your own certificate. For tunnels, see remote database access.
Which settings are worth changing first?
Raise shared_buffers, tell the planner how much cache the OS provides with effective_cache_size, and log slow statements.
ALTER SYSTEM SET shared_buffers = '512MB';
ALTER SYSTEM SET effective_cache_size = '1536MB';
ALTER SYSTEM SET work_mem = '16MB';
ALTER SYSTEM SET log_min_duration_statement = '500ms';Run these in sudo -u postgres psql; they land in postgresql.auto.conf. A common starting point is a quarter of RAM for shared_buffers and about three quarters for effective_cache_size (the numbers above suit 2 GB). shared_buffers takes effect after a restart; the others after SELECT pg_reload_conf();. Keep work_mem modest because each sort in each connection can use that much.
How do you verify, back up and upgrade PostgreSQL?
Connect as your application role over TCP to prove the whole path works, schedule pg_dump, and use pg_upgradecluster or pg_upgrade for major version jumps.
psql "host=127.0.0.1 dbname=appdb user=appuser" -c "SELECT version(), current_user;"
pg_dump -h 127.0.0.1 -U appuser -Fc appdb > appdb-$(date +%F).dump
sudo journalctl -u postgresql -n 40 --no-pagerOn Debian and Ubuntu, installing a new major version next to the old one and running sudo pg_upgradecluster 17 main moves the data across and shifts ports. On Rocky and Alma, run the new version's pg_upgrade with both data directories while both servers are stopped. Minor releases come through ordinary updates. Details and restore drills are in backing up PostgreSQL, and once the server is up, PostgreSQL data types is a good next read.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-2 | small app or staging database | 1 vCPU, 2 GB RAM, 50 GB NVMe | $7.20/mo | $43.20 for 6 months |
| A2-4 | production app with a warm cache | 2 vCPU, 4 GB RAM, 100 GB NVMe | $14.40/mo | $43.20 for 3 months |
| A4-8 | larger data sets and reporting | 4 vCPU, 8 GB RAM, 180 GB NVMe | $28.80/mo | $86.40 for 3 months |
Questions about Install PostgreSQL
Which PostgreSQL version should I install in 2026?
For a new project, the current major release from PGDG (18 at the time of writing) or the version your distro ships if it is 16 or newer. Check your framework's supported versions first.
Why does psql say peer authentication failed?
You connected over the local socket as a Linux user whose name differs from the database role. Use sudo -u postgres psql, or connect with -h 127.0.0.1 so a password line in pg_hba.conf applies.
Do I need to restart after editing pg_hba.conf?
No. A reload with SELECT pg_reload_conf(); or sudo systemctl reload postgresql is enough. Only some postgresql.conf settings such as listen_addresses and shared_buffers need a restart.
Can I run two PostgreSQL versions on one server?
Yes. Debian and Ubuntu manage several clusters with pg_lsclusters, each on its own port. PGDG packages on Rocky and Alma install into versioned paths and units such as postgresql-17 and postgresql-18.
Is it safe to expose port 5432?
Only to fixed addresses you trust, with hostssl lines and strong passwords. For admin work from a laptop, an SSH tunnel keeps the port closed and needs no extra rules.
How much memory does PostgreSQL need?
It runs on 1 GB for small projects. 2 to 4 GB lets shared_buffers and the OS cache hold a typical app database, which cuts disk reads sharply.
More in Databases
- Install MySQL 8
- Install MariaDB
- Install MongoDB
- PostgreSQL JSON, arrays and special types
- PostgreSQL views, triggers and CTEs
- PostgreSQL full-text search and encryption
- MySQL keys, indexes and full-text search
- MySQL JSON, views and stored procedures
- Apache Solr search server
- Install Redis or Valkey
- Install Memcached
- Apache Cassandra cluster
- Install Apache CouchDB
- InfluxDB for time series
- Install Elasticsearch
- Back up and restore MySQL and MariaDB
- Back up and restore PostgreSQL
- MySQL users, roles and passwords
- PostgreSQL roles and permissions
- Repair a crashed MySQL table
- PostgreSQL streaming replication
- MongoDB replica set
- PostGIS for location data
- Database insight with PgHero
- Couchbase Server
- ArangoDB multi-model database
- Remote database access over SSH