Installing PostgreSQL

1. Installing on Linux

DistroCommandNotes
Debian/Ubuntusudo apt install postgresql-17 postgresql-contrib-17Add PGDG repo for latest
RHEL/Fedorasudo dnf install postgresql17-server postgresql17-contribRequires PGDG yum repo
Archsudo pacman -S postgresqlLatest stable in core
PGDG Reposudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'Official upstream packages

2. Installing on macOS

MethodCommand
Homebrewbrew install postgresql@17
Start servicebrew services start postgresql@17
Postgres.appDownload GUI installer from postgresapp.com
EDB installerGraphical installer from enterprisedb.com

3. Installing on Windows

StepAction
DownloadEDB installer from enterprisedb.com/downloads
ComponentsServer, pgAdmin 4, Stack Builder, Command Line Tools
Data dirDefault: C:\Program Files\PostgreSQL\17\data
PortDefault 5432
LocaleMatch OS or set explicitly (e.g. en_US.UTF-8)
ServiceAuto-registered as postgresql-x64-17

4. Verifying Installation

CheckCommand
Server versionpostgres --version
Client versionpsql --version
Service statussystemctl status postgresql
Connect testsudo -u postgres psql -c "SELECT version();"

5. Understanding PostgreSQL Components

ComponentRole
postgresServer daemon (postmaster)
psqlInteractive terminal client
initdbCreates a new database cluster
pg_ctlStart/stop/restart server, init cluster
pg_dump / pg_restoreLogical backup/restore
pg_basebackupPhysical base backup for replication/PITR
pg_upgradeIn-place major version upgrade
pgAdminWeb-based admin GUI

6. Installing Development Tools

PackagePurpose
postgresql-server-dev-17Headers for building extensions
libpq-devClient library headers (libpq)
postgresql-contribExtra extensions (pg_stat_statements, etc.)
pgxnclientInstall community extensions

7. Setting Up System User

sudo useradd -m -s /bin/bash postgres
sudo passwd postgres
sudo -iu postgres            # become postgres user
psql                         # open psql as superuser
DetailValue
OS userpostgres (created by package)
DB superuserpostgres (peer-mapped)
Home dir/var/lib/postgresql

8. Configuring System Service

Actionsystemd Command
Enable on bootsudo systemctl enable postgresql
Startsudo systemctl start postgresql
Restartsudo systemctl restart postgresql
Statussudo systemctl status postgresql
Logsjournalctl -u postgresql -f

9. Installing Contrib Modules

ExtensionPurpose
pg_stat_statementsQuery performance tracking
pgcryptoCryptographic functions
uuid-osspUUID generation
hstoreKey/value column type
pg_trgmTrigram fuzzy text search
postgres_fdwForeign data wrapper for remote PG
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
CREATE EXTENSION IF NOT EXISTS pgcrypto;

10. Upgrading PostgreSQL Versions

ToolUse Case
pg_upgradeFast in-place major-version upgrade (hard links)
pg_dumpall + restoreCross-platform / risky upgrades
Logical replicationZero-downtime version upgrade
Minor versionsapt/dnf install + restart (no schema change)
pg_upgrade \
  -b /usr/lib/postgresql/16/bin \
  -B /usr/lib/postgresql/17/bin \
  -d /var/lib/postgresql/16/main \
  -D /var/lib/postgresql/17/main \
  --link --check       # dry run first, drop --check to run
Warning: Always take a full backup before pg_upgrade --link; old cluster becomes unusable.