This project provides a Docker Compose configuration to run a PostgreSQL database and pgAdmin in a containerized environment. The setup includes a Makefile for managing the lifecycle of the PostgreSQL container using simple commands.
Ensure you have the following installed on your system:
The PostgreSQL and pgAdmin services are defined in the docker-compose.yml file with the following settings:
- Image:
postgres:16 - Container Name:
postgres-db - Environment Variables (stored in
.envfile):POSTGRES_USER: Username for PostgreSQL authentication.POSTGRES_PASSWORD: Password for PostgreSQL authentication.POSTGRES_DB: Name of the database to be created.POSTGRES_PORT: Port for PostgreSQL.
- Ports: Exposes port
5432(configurable via.env). - Volumes: Data persistence is managed using a Docker volume
postgres_data. - Networks: The service is attached to a custom Docker network
mynetwork. - Health Check: The database service is monitored using
pg_isreadyto ensure readiness before dependent services start.
- Image:
dpage/pgadmin4 - Container Name:
pgadmin - Environment Variables (stored in
.envfile):PGADMIN_DEFAULT_EMAIL: Admin email for pgAdmin login.PGADMIN_DEFAULT_PASSWORD: Admin password for pgAdmin login.PGADMIN_PORT: Port for accessing pgAdmin.
- Ports: Exposes port
5050(configurable via.env). - Depends on:
db, and only starts when PostgreSQL is healthy.
Contains environment variables for PostgreSQL and pgAdmin configurations.
Defines the PostgreSQL and pgAdmin services, including networking, volumes, and health checks.
A set of commands to manage the Docker Compose lifecycle efficiently.
Ensures sensitive files, temporary files, and Docker artifacts are not committed to version control.
Copy the .env.example file to .env and update it with your credentials:
cp .env.example .envTo start the PostgreSQL and pgAdmin containers in detached mode, run:
make upIf you make changes to the configuration and need to rebuild the containers, use:
make rebuildTo stop the containers and rebuild them:
make zapIf you need to remove all volumes and start fresh, use:
make .zapWarning: This will delete all stored data.
To stop and remove the containers without deleting volumes, run:
make downTo remove all volumes associated with the containers:
make volumesWarning: This permanently deletes all stored database data.
- URL:
http://localhost:5050(or as configured in.env) - Login: Use the email specified in
PGADMIN_DEFAULT_EMAIL - Password: Use the password specified in
PGADMIN_DEFAULT_PASSWORD
You can connect to the PostgreSQL instance using any PostgreSQL client. The default connection parameters are:
- Host:
localhost - Port:
5432(or as configured in.env) - Username:
postgres_username - Password:
postgres_password - Database:
postgres_db_name
To connect via the command line, use:
docker exec -it postgres-db psql -U postgres_username -d postgres_db_name- Ensure Docker is running before executing the commands.
- If you face permission issues, try running commands with
sudo. - If the database is not persisting data, ensure the
postgres_datavolume is correctly mounted. - pgAdmin may not start if PostgreSQL is not healthy. Ensure PostgreSQL is running before starting pgAdmin.
This setup is open-source and free to use for any project.