Installing PostgreSQL
In this chapter, we will walk through the process of installing PostgreSQL on different platforms including Windows, macOS, Linux, and Docker. By the end of this tutorial, you will be able to set up a PostgreSQL server on your local machine and connect to it using the command-line tool psql.
System Requirements
PostgreSQL is lightweight and runs on most modern operating systems. Before installation, make sure your system meets the following minimum requirements:
- At least 1 GB RAM (2 GB recommended for development)
- 200 MB of disk space for installation
- Windows 10+, macOS 10.13+, or Linux (any recent distribution)
- Administrator/root access for installation
Installation on Windows
- Download the official installer from the PostgreSQL Windows download page.
- Run the installer and follow the wizard. You can choose the default options.
- During installation, you will be prompted to set a password for the postgres superuser. Remember this password—it is required for database access.
- Select the components to install:
- PostgreSQL Server
- pgAdmin (GUI management tool)
- Command Line Tools
- Once installation completes, PostgreSQL will run as a Windows service.
You can open pgAdmin (a graphical interface) or use the psql tool from the command prompt.
Installation on macOS
Method 1: Using Homebrew (Recommended)
brew update
brew install postgresql
# Start PostgreSQL service
brew services start postgresql
# Verify installation
psql --versionMethod 2: Using EnterpriseDB Installer
- Download the installer from the PostgreSQL macOS page.
- Run the package installer and follow the wizard.
- Set the superuser password during installation. PostgreSQL will be installed under
/Library/PostgreSQL.
Installation on Linux
PostgreSQL is available in the default repositories of most Linux distributions.
Ubuntu/Debian
# Update package list
sudo apt update
# Install PostgreSQL
sudo apt install postgresql postgresql-contrib
# Check service status
sudo systemctl status postgresql
# Switch to postgres user and start psql
sudo -i -u postgres
psqlCentOS/RHEL
# Enable PostgreSQL repository
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Disable built-in PostgreSQL module
sudo dnf -qy module disable postgresql
# Install PostgreSQL server
sudo dnf install -y postgresql16-server
# Initialize database
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
# Start service
sudo systemctl enable --now postgresql-16Using Docker
Docker makes it easy to run PostgreSQL without installing it directly on your system.
# Pull PostgreSQL image
docker pull postgres:16
# Run container
docker run --name mypostgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:16
# Connect using psql inside container
docker exec -it mypostgres psql -U postgresThis approach is great for testing and development because you can spin up and destroy containers without affecting your host machine.
Verifying Installation
Once installed, you can verify the PostgreSQL version with:
psql --version
To connect to PostgreSQL, use the psql shell:
psql -U postgres -d postgres
If the connection succeeds, you will see the PostgreSQL prompt (postgres=#).
Common Post-Installation Steps
- Create a new role/user:
CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword';
- Create a database:
CREATE DATABASE mydb OWNER myuser;
- Grant privileges:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Troubleshooting Installation
- Port Conflict: Ensure port 5432 is not being used by another service.
- Permission Issues: On Linux, always start PostgreSQL using the
postgressystem user. - Service Not Starting: Check logs at
/var/log/postgresqlor/var/lib/pgsql/data/.
Conclusion
Installing PostgreSQL is straightforward regardless of the operating system. On Windows and macOS, graphical installers like EnterpriseDB make it simple, while Linux users typically install through package managers. For developers who prefer containerized environments, Docker provides an easy way to set up PostgreSQL instances.
In the next chapter, we will learn how to create databases in PostgreSQL and manage them effectively.