Installing PostgreSQL and pgAdmin as Containers
Containerizing your development environment provides a lightweight, consistent, and reproducible setup. In this guide, we’ll walk through the process of installing PostgreSQL 16 and pgAdmin 4 as Docker containers, enabling you to manage your PostgreSQL databases efficiently.
Prerequisites
- Docker installed on the system.
- Basic knowledge of Docker commands.
Step 1: Pull the PostgreSQL and pgAdmin Docker Images
First, let’s pull the official Docker images for PostgreSQL 16 and pgAdmin 4 from Docker Hub:
docker pull postgres:16
docker pull dpage/pgadmin4
Step 2: Create a Docker Network
To allow PostgreSQL and pgAdmin containers to communicate, create a custom Docker network:
docker network create pgnetwork
Step 3: Run the PostgreSQL Container
Next, start the PostgreSQL container. Be sure to specify the Docker network you created:
docker run --name postgres_16 --network pgnetwork \
-e POSTGRES_PASSWORD=postgresql \
-d -p 5432:5432 \
-v /host-path/postgresql_16/data:/var/lib/postgresql/data \
postgres:16
-v: Maps a directory on your host machine to the container’s data directory to persist data.
Step 4: Run the pgAdmin 4 Container
Now, run the pgAdmin 4 container, also attaching it to the same Docker network:
docker run --name my_pgadmin --network pgnetwork \
-e PGADMIN_DEFAULT_EMAIL=[email protected] \
-e PGADMIN_DEFAULT_PASSWORD=postgresql \
-d -p 8080:80 dpage/pgadmin4
PGADMIN_DEFAULT_EMAIL: Email for the pgAdmin login.
PGADMIN_DEFAULT_PASSWORD: Password for the pgAdmin login.
Step 5: Connect pgAdmin to PostgreSQL
- Access pgAdmin 4 by navigating to http://localhost:8080 in your browser.
- Log in using the email and password you set.
- Add a new server:
- Name: PostgreSQL 16
- Host name/address: postgres_16 (the name of your PostgreSQL container)
- Port: 5432
- Username: postgres
- Password: postgresql
Once configured, pgAdmin will connect to your PostgreSQL instance, and you can manage your databases from the pgAdmin interface.
Step 6: Managing Containers and Networks
- List running containers:
docker ps
docker stop <container_name>
docker rm <container_name>
List networks:
docker network ls
Conclusion
By running PostgreSQL and pgAdmin as Docker containers, you can maintain a clean, isolated environment for your database management tasks. This setup is portable and easy to replicate, making it ideal for both development and testing environments.