ReviseAlgo Logo

Setting Up PostgreSQL

Installing PostgreSQL

How to install PostgreSQL locally on macOS, Linux, and Windows.

Interview: Interviewers expect candidates to understand the PostgreSQL installation process, version management, and configuration basics — especially for backend roles involving database operations and deployment.

Last Updated: June 12, 2026 15 min read

1. Introduction

PostgreSQL is the most advanced open-source relational database. Before writing queries, you need a working local installation. This topic covers installing PostgreSQL on macOS, Linux, and Windows, understanding version selection, and verifying your setup.

2. Why It Matters

Interviewers expect candidates to understand the PostgreSQL installation process, version management, and configuration basics. A broken or outdated installation blocks all development work, and production incidents often require connecting to remote databases via psql over SSH.

  • Version awareness: Running an EOL (End of Life) version means no security patches — a production risk.
  • Reproducible environments: Docker-based installs ensure dev, staging, and production use identical PostgreSQL versions.
  • Platform fluency: Backend engineers must install, configure, and troubleshoot PostgreSQL on any OS.

3. Real-World Analogy

Installing PostgreSQL is like setting up a development toolchain (Node.js, Python, Go). You choose a version manager (Homebrew, apt, Docker), install the runtime, verify it works, and configure environment variables. Just as you wouldn't run a production app on an unsupported Node.js version, you shouldn't run PostgreSQL 13 in production.

4. How It Works

PostgreSQL is a client-server application. The server (postgres process) manages data files, handles connections, and executes queries. The client (psql, pgAdmin, your application) connects over TCP to send SQL commands. Installation sets up both components plus background services.

  • Server process: Runs as a background daemon (managed by systemctl on Linux, brew services on macOS).
  • Data directory: Stores all database files (default: /var/lib/postgresql/data or /usr/local/var/postgres).
  • Client tools: psql (CLI), pgAdmin (GUI), pg_dump/pg_restore (backup utilities).
  • Default port: 5432 (TCP).

5. Internal Architecture

PostgreSQL uses a multi-process architecture (not threads). Each client connection spawns a dedicated backend process. A master process (postmaster) manages shared memory, background writer, WAL writer, and autovacuum processes.

6. Visual Explanation

7. Practical Example

macOS installation via Homebrew:

Linux (Ubuntu) installation with official repository:

8. Common Mistakes

Common Pitfall

Installing PostgreSQL via the distro's default package manager on Ubuntu often gives you an outdated version (e.g., PostgreSQL 14 when 17 is current). Always add the official PostgreSQL APT repository to get the latest stable release with security patches.

  • Not starting the service: On macOS, brew install does not auto-start PostgreSQL. You must run brew services start postgresql@17.
  • PATH issues: After installation, psql may not be found if the PostgreSQL bin directory isn't in your PATH. Homebrew typically adds it automatically; Linux may require manual PATH configuration.
  • Running as superuser: Never develop using the postgres superuser account. Create a dedicated development user.

9. Quick Quiz

Q1: What is the default PostgreSQL port?

Port 5432. If running multiple PostgreSQL instances, each needs a different port.

Q2: Why should you use Docker for PostgreSQL development?

Docker keeps your host OS clean, enables easy version switching (just change the image tag), and ensures dev/prod use identical PostgreSQL versions.

10. Scenario-Based Challenge

Scenario

Your team needs to test a new feature that requires PostgreSQL 17, but production runs PostgreSQL 16. How would you set up a local environment to test both versions simultaneously without conflicts? What Docker commands would you use?

11. Debugging Exercise

You installed PostgreSQL but psql returns this error:

Fix: The PostgreSQL server is not running. Start it with brew services start postgresql@17 (macOS) or sudo systemctl start postgresql (Linux).

12. Interview Questions

Q: What's the difference between PostgreSQL 16 and 17?

Each annual release adds performance improvements, new SQL features, and security patches. PG17 added improved parallelism, better JSON support, and query optimization improvements. Always check the release notes for specifics.

Q: How do you manage multiple PostgreSQL versions on one machine?

Use Homebrew version pinning (brew install postgresql@16 postgresql@17) with different ports, or Docker containers with version-specific images. Never mix data directories between versions.

Q: What does the data directory contain?

Database files, WAL (Write-Ahead Log) files, configuration files (postgresql.conf, pg_hba.conf), and system catalogs. The data directory is managed by initdb and should never be manually edited.

13. Production Considerations

  • Version lifecycle: PostgreSQL supports each major version for 5 years. Plan upgrades before EOL to maintain security patches.
  • Docker for CI/CD: Use Docker PostgreSQL images in GitHub Actions or GitLab CI for integration testing. Pin to a specific version tag (postgres:17.2, not postgres:latest).
  • Docker volumes: Always use named volumes to persist data across container restarts. Without volumes, all data is lost when the container stops.
  • Managed services: In production, prefer managed PostgreSQL (AWS RDS, Google Cloud SQL, Supabase) over self-hosted for automatic backups, patching, and high availability.

Use Cases

Development setup: Installing PostgreSQL locally to build and test applications before deploying to production

CI/CD pipelines: Using Docker PostgreSQL images in GitHub Actions or GitLab CI for integration testing

Multi-version testing: Running multiple PostgreSQL versions side-by-side to test application compatibility before upgrading production

Common Mistakes

Installing the distro default version instead of the latest — Ubuntu 22.04 ships PostgreSQL 14 by default, which is multiple versions behind

Forgetting to start the PostgreSQL service after installation — on macOS, brew install does not auto-start the service; you must run brew services start

Not setting up the PATH correctly — psql may not be found in terminal sessions if the PostgreSQL bin directory is not in your PATH environment variable