docker
postgres
pgadmin
local connection
database management

docker postgres pgadmin local connection

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

yaml
1version: '3.8'
2services:
3  postgres:
4    image: postgres:latest
5    container_name: my_postgres
6    environment:
7      POSTGRES_USER: admin
8      POSTGRES_PASSWORD: secret
9      POSTGRES_DB: mydb
10    ports:
11      - "5432:5432"
12    volumes:
13      - postgres_data:/var/lib/postgresql/data
14
15  pgadmin:
16    image: dpage/pgadmin4
17    container_name: my_pgadmin
18    environment:
19      PGADMIN_DEFAULT_EMAIL: [email protected]
20      PGADMIN_DEFAULT_PASSWORD: secret
21    ports:
22      - "5050:80"
23
24volumes:
25  postgres_data: {}

Explanation

  • PostgreSQL: We use the official postgres image. The environment variables set up a default database, a user, and a password. We also map it to the default PostgreSQL port 5432.
  • pgAdmin: The dpage/pgadmin4 image is pulled for pgAdmin. We set up an admin email and password for login. The service is exposed on port 5050.
  • Volumes: A named volume postgres_data is 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

  1. After bringing up the containers using docker-compose up -d, navigate to http://localhost:5050 in your browser.
  2. Log into pgAdmin using the credentials specified in the docker-compose.yml file (default email: [email protected], password: secret).
  3. 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
  4. Click 'Save' to establish the connection.

Key Points Summary

TopicDetails
Docker Image for Postgrespostgres:latest
Docker Image for pgAdmindpage/pgadmin4
PostgreSQL Port5432
pgAdmin User CredentialsUsername: [email protected] Password: secret
PostgreSQL User CredentialsUsername: admin Password: secret
Volume Usagepostgres_data to persist DB data
Network ConnectivityThrough 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.


Course illustration
Course illustration

All Rights Reserved.