How to Install PostgreSQL
PostgreSQL's installation process varies by platform, but the core steps are the same everywhere: install the server package, initialize the data directory, start the service, configure authentication, and verify the connection. The most important decision is whether to use your distribution's bundled PostgreSQL version or the official PostgreSQL repository. We recommend the official repository because it always has the latest stable release and receives security patches faster.
Step 1: Choose Your Installation Method
PostgreSQL can be installed through several methods depending on your platform and preferences. Native package managers (apt, yum/dnf, Homebrew) are the most common and recommended approach. The official PostgreSQL Global Development Group (PGDG) repositories provide the latest stable version for Debian, Ubuntu, Red Hat, Rocky Linux, AlmaLinux, Fedora, and SUSE. Docker containers are convenient for development and testing but require additional configuration for production persistence and performance. On macOS, Postgres.app provides a self-contained bundle that runs without Homebrew. On Windows, the official EDB installer provides a graphical setup wizard.
For production Linux servers, always use the PGDG repository rather than your distribution's default package. Distribution packages are often one or two major versions behind, which means you miss performance improvements, security features, and bug fixes. The PGDG repository is maintained by the PostgreSQL community and tracks every supported release line.
Step 2: Install on Ubuntu or Debian
On Ubuntu and Debian, start by adding the official PostgreSQL apt repository. Install the prerequisite packages (curl, ca-certificates, gnupg), add the PGDG signing key, create the repository list file, and run apt update. Then install the latest PostgreSQL version with apt install postgresql. The installer automatically initializes the data directory, creates the postgres system user, and starts the PostgreSQL service.
After installation, verify the service is running with systemctl status postgresql. The default configuration listens only on localhost (127.0.0.1) and uses peer authentication for local connections, which means any system user can connect to a database with a matching username without a password. This is secure for single-server setups but needs adjustment for remote connections.
To install a specific version rather than the latest, specify the version number: apt install postgresql-16. The PGDG repository supports running multiple PostgreSQL versions simultaneously on the same server, each on a different port, which is useful for testing upgrades before committing.
Step 3: Install on Red Hat, Rocky, or AlmaLinux
On Red Hat-family distributions, install the PGDG yum repository RPM from yum.postgresql.org. Disable the default AppStream PostgreSQL module (which ships an older version) with dnf -qy module disable postgresql. Then install with dnf install postgresql16-server (replacing 16 with your target version).
Unlike Debian-based systems, Red Hat-family installations do not initialize the database cluster automatically. Run the initialization command (postgresql-16-setup initdb on RHEL/Rocky/Alma) to create the data directory and default configuration files. Then enable and start the service with systemctl enable postgresql-16 and systemctl start postgresql-16.
The default data directory on Red Hat-family systems is /var/lib/pgsql/16/data (version-specific). The main configuration files, postgresql.conf and pg_hba.conf, are located inside this data directory.
Step 4: Install on macOS
The simplest macOS installation method is Homebrew. Run brew install postgresql@16, then brew services start postgresql@16 to launch PostgreSQL as a background service that starts automatically on login. Homebrew installs the data directory under the Homebrew Cellar and provides the standard psql, pg_dump, and pg_restore command-line tools.
Postgres.app is an alternative that packages PostgreSQL as a standard macOS application. Download it from postgresapp.com, drag it to your Applications folder, and click Start. Postgres.app supports running multiple PostgreSQL versions simultaneously and includes PostGIS and other popular extensions. Add the Postgres.app bin directory to your PATH to use psql and other tools from the terminal.
For development on macOS, either method works well. Homebrew integrates better with other development tools managed through brew. Postgres.app is simpler if you want a self-contained application that you can stop and start from the menu bar.
Step 5: Install on Windows
Download the official installer from the PostgreSQL downloads page at postgresql.org/download/windows. The installer is provided by EDB (EnterpriseDB) and includes the PostgreSQL server, pgAdmin 4 (a web-based administration tool), the command-line tools, and Stack Builder for installing additional components like PostGIS.
Run the installer and follow the setup wizard. Choose your installation directory, data directory, superuser password, port number (default 5432), and locale. The installer creates a Windows service that starts PostgreSQL automatically on boot. After installation, you can connect using pgAdmin 4 (installed alongside PostgreSQL) or psql from the command prompt.
On Windows, PostgreSQL performs best when Windows Defender or other antivirus software is configured to exclude the PostgreSQL data directory from real-time scanning. Real-time file scanning on database files causes significant I/O overhead and can degrade performance noticeably under write-heavy workloads.
Step 6: Configure Authentication and Create Your First Database
PostgreSQL's authentication is controlled by the pg_hba.conf file (HBA stands for Host-Based Authentication). Each line specifies a connection type (local socket, TCP/IP host, SSL host), a database, a user, a client address range, and an authentication method. For local development, the default peer authentication (on Linux) or md5/scram-sha-256 password authentication is sufficient.
To create your first database, connect as the postgres superuser: sudo -u postgres psql on Linux, or psql -U postgres on macOS and Windows. Then create a new role (user) with CREATE ROLE myuser LOGIN PASSWORD 'yourpassword'; and a new database with CREATE DATABASE mydb OWNER myuser;. Connect to the new database with psql -U myuser -d mydb to verify everything works.
For applications, always create a dedicated database user with only the permissions the application needs. Never connect applications using the postgres superuser account. Use GRANT statements to give the application user SELECT, INSERT, UPDATE, and DELETE permissions on the specific tables it accesses, following the principle of least privilege.
Step 7: Tune Essential Settings
PostgreSQL's default configuration is conservative and designed to work on minimal hardware. For anything beyond a small development database, you should tune the key settings in postgresql.conf to match your server's resources.
Set shared_buffers to approximately 25% of total system RAM. This is PostgreSQL's dedicated buffer cache. Set effective_cache_size to approximately 75% of total RAM, which tells the query planner how much memory is available for caching (including the operating system's file cache). Set work_mem to a value appropriate for your concurrency level, typically between 4 MB and 64 MB per operation. Higher values speed up sorts and hash joins but multiply by the number of concurrent operations, so be careful on servers with many connections.
Set maintenance_work_mem to a larger value (256 MB to 1 GB) to speed up VACUUM, CREATE INDEX, and other maintenance operations. Enable wal_level = replica even if you are not using replication yet, because switching this setting later requires a restart and you may want replication in the future. After making changes, reload the configuration with SELECT pg_reload_conf(); or restart the service for settings that require a restart (shared_buffers, wal_level).
Use the official PGDG repository for the latest stable version on Linux, tune shared_buffers and effective_cache_size to match your hardware, and always create dedicated application users rather than connecting as the postgres superuser.