PostgreSQLbeginner
PostgreSQL Installation
Installing PostgreSQL for production from the official repositories, and the decisions to make before the first start.
Distribution packages often lag several major versions behind. For production, use the PostgreSQL Global Development Group (PGDG) repositories so you can choose the major version and receive minor-version security updates promptly.
Decisions to make before installing
- Major version. Pick a version with several years of support remaining. Upgrading major versions is a project; upgrading minor versions is a restart.
- Data directory location. Put
PGDATAon its own filesystem, sized and monitored separately from the root volume. A full root filesystem should not be able to stop the database, and a growing database should not fill the root filesystem. - Filesystem. ext4 or XFS with
noatime. Avoid network filesystems for the data directory. - Locale and encoding.
UTF8encoding. The collation chosen atinitdbtime affects index ordering, and changing it later requires reindexing every text index.
Install and initialise
sudo apt install -y postgresql-common && sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.shAdds the PGDG repository on Debian and Ubuntu. The equivalent for RHEL-family systems is the
pgdg-redhat-repo RPM.
sudo apt install -y postgresql-17
# Initialise a cluster on a dedicated filesystem with an explicit collation.
sudo pg_dropcluster --stop 17 main
sudo pg_createcluster 17 main \
--datadir=/srv/pgdata/17/main \
--locale=en_US.UTF-8 \
--encoding=UTF8
sudo pg_ctlcluster 17 main startImmediately after the first start
- Set a password for the
postgresrole, or configure certificate authentication. - Replace the default
pg_hba.confentries with specific host and network rules — see Connection Management. - Apply the production configuration and restart once, rather than incrementally.
- Configure WAL archiving before the database receives real data, so point-in-time recovery is available from the beginning.
- Create the application role with limited privileges, and use it from the application.