docker postgres pgadmin local connection
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker has revolutionized the way developers build, ship, and run applications by providing consistency across various computing environments. Combining Docker with databases like PostgreSQL allows for an efficient development setup. To ease database management, tools like pgAdmin can be used alongside. This article details how to establish a local connection between Dockerized PostgreSQL and pgAdmin, complete with technical insights and examples.
Prerequisites
Before we dive into setting up Docker containers for PostgreSQL and pgAdmin, ensure you have the following prerequisites:
- Docker and Docker Compose installed on your machine.
- Basic understanding of Docker and networking.
- Familiarity with PostgreSQL and pgAdmin.
Setting Up Dockerized PostgreSQL and pgAdmin
Docker Compose File
The simplest way to deploy PostgreSQL and pgAdmin together is by using Docker Compose. Below is an example docker-compose.yml file which sets up both services.
Explanation
- PostgreSQL: We use the official
postgresimage. The environment variables set up a default database, a user, and a password. We also map it to the default PostgreSQL port5432. - pgAdmin: The
dpage/pgadmin4image is pulled for pgAdmin. We set up an admin email and password for login. The service is exposed on port5050. - Volumes: A named volume
postgres_datais created to persist PostgreSQL data across container restarts.
Networking and Local Connection
Docker Compose automatically creates an isolated network for services defined in a compose file. This allows services to communicate with each other using service names as hostnames. In our case, pgAdmin can connect to PostgreSQL using the service name postgres.
Connecting pgAdmin to PostgreSQL
- After bringing up the containers using
docker-compose up -d, navigate tohttp://localhost:5050in your browser. - Log into pgAdmin using the credentials specified in the
docker-compose.ymlfile (default email:[email protected], password:secret). - To add a new server in pgAdmin, follow these steps:
- Go to 'Dashboard' and navigate to 'Create' > 'Server'.
- In the 'Create - Server' dialog:
- General Tab: Name your connection (e.g.,
Local Postgres). - Connection Tab:
- Hostname/Address:
postgres(the service name defined in Docker Compose). - Port:
5432 - Username:
admin - Password:
secret
- Click 'Save' to establish the connection.
Key Points Summary
| Topic | Details |
| Docker Image for Postgres | postgres:latest |
| Docker Image for pgAdmin | dpage/pgadmin4 |
| PostgreSQL Port | 5432 |
| pgAdmin User Credentials | Username: [email protected]
Password: secret |
| PostgreSQL User Credentials | Username: admin
Password: secret |
| Volume Usage | postgres_data to persist DB data |
| Network Connectivity | Through Docker Compose's default network |
Additional Considerations
Data Persistence
In Docker, persisting data across container restarts is crucial, especially for databases. Using volumes (like postgres_data in our example) prevents data loss when containers are rebuilt or restarted.
Security Practices
When setting up databases in Docker, consider these security best practices:
- Use environment variables to inject secrets and credentials securely.
- Limit exposed ports to only necessary services. For instance, keep PostgreSQL internal if it's only accessed by pgAdmin.
- Regularly update Docker images to mitigate vulnerabilities.
Scaling and Backup
For production setups, consider the following:
- Scaling: Use Docker Swarm or Kubernetes for scaling database and management services.
- Backup: Implement regular automated backups using tools like
pg_dump.
Conclusion
Using Docker to set up PostgreSQL and pgAdmin allows developers to quickly get started with database management in a consistent, isolated environment. By leveraging Docker Compose, we can effortlessly create, maintain, and scale our database infrastructure. Always remember to follow best practices for data persistence and security when working with containerized databases, thereby ensuring a reliable and efficient development workflow.
Related reading
- ''docker ps'' output formatting list only names of running containers
- docker pull always show Already exists for layers during pull even after deleting all images
- docker pull returns denied access forbidden from private gitlab registry
- Docker pull “unexpected EOF”
- Doctrine - How to print out the real sql, not just the prepared statement?
- Doctrine and composite unique keys
- docker push error denied requested access to the resource is denied
- docker push error denied requested access to the resource is denied

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.