Pick the method that matches your system. Each one gives you a running server on port 5432 and the psql client:
| System | Recommended method |
|---|---|
| Windows | The graphical installer linked from postgresql.org |
| Debian, Ubuntu | apt with the PostgreSQL project's own (PGDG) repository |
| RHEL, Rocky Linux, AlmaLinux, Fedora | dnf with the PGDG repository |
| Any machine with Docker | The official postgres image |
The commands below install PostgreSQL 18, the current major version at the time of writing. A new major version is released about once a year and each one is supported for five years, so replace 18 with the version you want. Check postgresql.org/support/versioning for the supported list.
Windows: the graphical installer
- Go to postgresql.org/download/windows and follow the link to the installer, which is built and hosted by EDB. Download the latest version for Windows x86-64.
- Run the
.exe. Administrator rights are required. - Work through the wizard:
- Installation directory. The default is
C:\Program Files\PostgreSQL\18. - Components. PostgreSQL Server, pgAdmin 4 (a graphical administration tool), Stack Builder (downloads optional add-ons) and Command Line Tools (
psql,pg_dumpand others). Keep the server and the command line tools; the other two are optional. - Data directory. Where the database files live. The default is a
datafolder under the installation directory. Put it on a disk with room to grow. - Password. This sets the password for the database superuser,
postgres. There is no recovery prompt later, so store it in a password manager. - Port. Leave it at 5432 unless something else is using it.
- Locale. The default uses the operating system locale. It becomes the default sort order and character classification for new databases. An existing database keeps the locale it was created with, so choose deliberately.
- Installation directory. The default is
- Finish the wizard. If it offers to launch Stack Builder, you can decline.
The server is installed as a Windows service named postgresql-x64-18 that starts automatically at boot. Check it and connect from PowerShell:
Get-Service postgresql-x64-18
& "C:\Program Files\PostgreSQL\18\bin\psql.exe" -U postgres
The installer does not add the bin folder to PATH. If typing psql gives "'psql' is not recognized as an internal or external command", add C:\Program Files\PostgreSQL\18\bin to the Path environment variable (System Properties → Environment Variables) and open a new terminal.
Debian and Ubuntu: apt with the PGDG repository
sudo apt install postgresql works without any setup, but it installs whichever version your distribution release shipped with. To choose the version and get current minor releases, add the PostgreSQL project's repository first:
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
sudo apt update
sudo apt install -y postgresql-18
The package creates a database cluster, starts it and enables it at boot. Verify:
pg_lsclusters
systemctl status postgresql
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
Configuration files are in /etc/postgresql/18/main/.
RHEL, Rocky Linux, AlmaLinux: dnf with the PGDG repository
The exact commands depend on the distribution version and CPU architecture; the form at postgresql.org/download/linux/redhat generates them for you. For version 9 of RHEL-compatible distributions on x86-64 they are:
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
Two differences from Debian catch people out:
- The package does not create a database cluster. Until you run the
postgresql-18-setup initdbstep, the service will not start. - The
module disableline turns off the distribution's own PostgreSQL module so it does not hide the PGDG packages.
The data directory, which also holds postgresql.conf and pg_hba.conf, is /var/lib/pgsql/18/data.
Docker
For development and testing, a container is the fastest route:
docker run --name pg18 \
-e POSTGRES_PASSWORD=change-me \
-p 5432:5432 \
-v pgdata:/var/lib/postgresql \
-d postgres:18
POSTGRES_PASSWORDis required. Without it the container exits with "Database is uninitialized and superuser password is not specified".- The named volume keeps your data when the container is removed. The mount point changed with the version 18 image: use
/var/lib/postgresqlfor 18 and later, and/var/lib/postgresql/datafor 17 and earlier. With the wrong path your data does not end up in the named volume, and you find out when you recreate the container. - Pin a major version (
postgres:18), notlatest. A new major version cannot read an older version's data directory.
Open a shell in the database with:
docker exec -it pg18 psql -U postgres
First connection
How you get in the first time depends on the platform, because the default authentication rules differ.
Windows and Docker: connect as postgres with the password you chose.
Linux packages: the postgres database user has no password. Local connections over the Unix socket use peer authentication, which lets an operating system user connect as the database user of the same name. So switch to the postgres OS user:
sudo -u postgres psql
Once connected, check the version and create a role and a database for your application, so that it does not run as the superuser:
SELECT version();
CREATE ROLE app_user LOGIN PASSWORD 'replace-with-a-strong-password';
CREATE DATABASE app_db OWNER app_user;
Leave psql with \q, then test the new account over TCP:
psql -h localhost -U app_user -d app_db
Useful psql commands: \l lists databases, \du lists roles, \dt lists tables, \conninfo shows how you are connected, and \? lists the rest.
Allowing connections from other machines
A fresh installation listens only on localhost. To accept remote connections, two files need a change. Find them from psql:
SHOW config_file;
SHOW hba_file;
In postgresql.conf, set the interfaces to listen on:
listen_addresses = '*'
In pg_hba.conf, add a line that says who may connect from where. Keep the address range as narrow as you can:
host app_db app_user 10.0.1.0/24 scram-sha-256
Changing listen_addresses needs a restart (sudo systemctl restart postgresql on Debian and Ubuntu, postgresql-18 on RHEL-family systems, or restart the Windows service). Changes to pg_hba.conf only need a reload: SELECT pg_reload_conf();. Also open port 5432 in the host firewall for the same address range. Do not expose the port to the internet.
Common post-install problems
| Message | What it means and what to do |
|---|---|
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory | The server is not running, or it is on another port. Check the service status. On RHEL-family systems, check that you ran the initdb step. |
FATAL: Peer authentication failed for user "postgres" | You ran psql -U postgres as a different OS user. Use sudo -u postgres psql, or connect with -h localhost as a role that has a password. |
FATAL: password authentication failed for user "postgres" | Wrong password. On Linux packages postgres has none until you set one with \password postgres. |
FATAL: role "alice" does not exist or database "alice" does not exist | With no options, psql uses your OS user name as both the role and the database. Pass -U and -d. |
Connection refused from another machine | listen_addresses is still localhost, or a firewall is blocking 5432. |
FATAL: no pg_hba.conf entry for host "10.0.1.7", user "app_user", database "app_db" | The server is reachable but no pg_hba.conf line matches. Add one and reload. |
| Windows installer offers port 5433 | Another PostgreSQL instance already holds 5432. Either accept the new port and connect with -p 5433, or remove the old instance. |
When the cause is not obvious, read the server log: /var/log/postgresql/ on Debian and Ubuntu, the log directory inside the data directory on RHEL-family systems and Windows, and docker logs pg18 for the container.
Updating and removing
Minor releases (18.1 to 18.2) are bug and security fixes that keep the data format. Install them through the normal package update, or run the newer Windows installer over the existing installation, then restart the service.
Major versions (17 to 18) change the on-disk format. Installing the new packages does not convert your data; you move it with pg_upgrade or with pg_dump and restore. Different major versions can be installed side by side on different ports while you do that.
To remove PostgreSQL, uninstall the package or use Apps in Windows Settings. The Windows uninstaller leaves the data directory in place; delete it yourself only once you are sure you no longer need the databases. Take a pg_dumpall backup before removing anything.
Further reading
Get the weekly commit
New database deep dives every week.
