If you’ve ever struggled with setting up PostgreSQL or wanted a quick, isolated environment to manage databases, running PostgreSQL and PgAdmin as Docker containers is a fantastic solution. This setup is lightweight, portable, and easy to manage, making it perfect for development or testing environments.
In this post, I’ll Walk you through installing and using PostgreSQL with PgAdmin inside Docker containers.
Why Use Docker for PostgreSQL and PgAdmin?
Using Docker offers several advantages over traditional installation methods:
- Isolation: Keeps database separate from your local system.
- Portability: Easy to replicate across environments.
- Simple Setup: Quick to start or remove.
- Clean Environment: No local installation needed.
- Efficient: Lightweight compared to VMs.
Step-by-Step Guide
1. Install Docker
If you don’t already have Docker installed, head over to the official installation guide:
👉 Install | Docker Docs
2. Run the PostgreSQL Container
Open your terminal and run:
docker run --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgresBreaking it down:
--name postgres: Names the containerpostgres.-e POSTGRES_PASSWORD=password: Sets the PostgreSQL password.-p 5432:5432: Maps PostgreSQL’s port (5432) to the host system.-d postgres: Runs the container in detached mode.
3. Run the PgAdmin Container
Now, let’s run PgAdmin, a popular web-based GUI for managing PostgreSQL:
docker run --name pgadmin -p 5050:80 \
-e PGADMIN_DEFAULT_EMAIL=admin@admin.com \
-e PGADMIN_DEFAULT_PASSWORD=admin \
-d dpage/pgadmin4Breaking it down:
--name pgadmin: Names the containerpgadmin.-p 5050:80: Maps container port 80 to host port 5050.-e PGADMIN_DEFAULT_EMAIL=admin@admin.com: Default login email.-e PGADMIN_DEFAULT_PASSWORD=admin: Default login password.-d dpage/pgadmin4: Runs PgAdmin in detached mode.
4. Verify Containers Are Running
Check running containers with:
docker psYou should see both postgres and pgadmin containers in the list.
5. Get PostgreSQL Container’s IP Address
To connect PgAdmin with PostgreSQL, you’ll need the container’s IP address:
docker inspect postgres | grep "IPAddress"You’ll get something like:
"IPAddress": "172.17.0.2"6. Connect PgAdmin to PostgreSQL
Open your browser and go to: http://localhost:5050
Log in with:
- Email:
admin@admin.com - Password:
admin
Add a new server in PgAdmin:
- Go to Add New Server.
- In the General tab, give your server a name.
- In the Connection tab, fill in:
- Host name/address:
172.17.0.2(the PostgreSQL container’s IP). - Port:
5432 - Username:
postgres - Password:
password
- Host name/address:
One this is done, you now have PostgreSQL running in a container, fully manageable through PgAdmin in your browser.
Wrapping Up
Dockerizing PostgreSQL and PgAdmin provides a streamlined, clutter-free way to manage databases for development and testing. Instead of wrestling with local installations, you can spin up fully isolated, portable environments in seconds. With just a few commands, you’ll have a powerful database and an intuitive web interface ready to go — and just as easily, you can tear it all down when you’re done.