If you're looking for a fast and hassle-free way to install
PostgreSQL, Docker is your best friend. Whether you're working on a development project or just want a quick way to run a PostgreSQL database locally, Docker allows you to get up and running without installing PostgreSQL directly on your system.
In this article, we'll walk through
how to install and run PostgreSQL using Docker, step by step. No complicated setup, no heavy installers—just clean, lightweight containerized PostgreSQL that runs anywhere.
What You’ll Learn
- Why use Docker to run PostgreSQL
- How to pull the official PostgreSQL Docker image
- How to run PostgreSQL in a Docker container
- How to configure ports, username, password, and volumes
- How to connect to the database from your app or a database tool
Why Use Docker for PostgreSQL?
Here are some of the biggest benefits of using Docker for PostgreSQL:
- No need to install PostgreSQL directly on your system
- Easy to start, stop, and remove when you no longer need it
- You can run multiple versions side by side
- Perfect for isolated dev and test environments
- Works the same on all machines—great for teams
Step 1: Install Docker (If You Haven’t Yet)
First, make sure Docker is installed on your machine.
macOS or Windows:
Download Docker Desktop from the official site:
https://www.docker.com/products/docker-desktop
Linux (Ubuntu):
sudo apt update && sudo apt install docker.io
After installation, test it with:
docker --version
Step 2: Pull the PostgreSQL Docker Image
Docker Hub hosts an official PostgreSQL image that you can use.
Run the following command in your terminal:
docker pull postgres
This will download the latest version of PostgreSQL. If you want a specific version, just specify it:
docker pull postgres:15
Tip: You can check available tags here: https://hub.docker.com/_/postgres
Step 3: Run PostgreSQL in a Docker Container
Here’s a basic command to start a PostgreSQL container:
docker run --name my-postgres \
-e POSTGRES_USER=tutorialjava \
-e POSTGRES_PASSWORD=tutorialjava \
-e POSTGRES_DB=tutorialjava \
-p 5432:5432 \
-d postgres
Breakdown:
--name my-postgres
→ name of your container
-e POSTGRES_USER
→ sets the username
-e POSTGRES_PASSWORD
→ sets the password
-e POSTGRES_DB
→ creates a database with this name
-p 5432:5432
→ maps PostgreSQL port to your local machine
-d
→ runs the container in the background
postgres
→ uses the latest PostgreSQL image
Step 4: Persist Data Using Volumes (Optional but Recommended)
By default, if you stop and remove the container, your data is lost. To keep data between restarts, use volumes:
Create Volume
docker volume create pgdata
Start PostgreSQL
docker run --name my-postgres \
-e POSTGRES_USER=tutorialjava \
-e POSTGRES_PASSWORD=tutorialjava \
-e POSTGRES_DB=tutorialjava \
-p 5432:5432 \
-v pgdata:/var/lib/postgresql/data \
-d postgres
This stores the database files in a Docker-managed volume named
pgdata
.
Step 5: Verify That PostgreSQL Is Running
To check if the container is running:
docker ps
You should see something like this:
CONTAINER ID IMAGE COMMAND STATUS PORTS
1234abcd postgres "docker-entrypoint..." Up 5 seconds 0.0.0.0:5432->5432/tcp
Step 6: Connect to PostgreSQL
Using psql (PostgreSQL CLI)
If you have
psql
installed locally:
psql -h localhost -U tutorialjava -d tutorialjava
Then enter your password (
secret123
in our example).
Using a GUI (e.g., DBeaver, pgAdmin, DataGrip)
- Host:
localhost
- Port:
5432
- User:
admin
- Password:
secret123
- Database:
mydatabase
You’ll be able to view tables, run queries, and manage your database just like a regular PostgreSQL installation.
Useful Docker Commands
Command |
Description |
docker stop my-postgres |
Stop the container |
docker start my-postgres |
Start it again |
docker rm my-postgres |
Remove the container |
docker volume ls |
List volumes |
docker volume rm pgdata |
Remove the volume |
Best Practices
- Always use volumes in development to persist your data.
- Use
.env
files or Docker Compose for easier environment management (especially in teams).
- Avoid using root-level passwords in production setups.
- Regularly back up your volumes if needed.
Cleaning Up
If you’re done testing and want to remove everything:
docker stop my-postgres && docker rm my-postgres && docker volume rm pgdata
This will completely remove the container and its data.
Bonus: Using Docker Compose (Optional)
If you prefer using a
docker-compose.yml
file, here’s a sample:
version: '3.8'
services:
postgres:
image: postgres:15
container_name: my-postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret123
POSTGRES_DB: mydatabase
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Then start it with:
docker-compose up -d
image quote pre code