Sigma
Integral
πPi
E=mc²Einstein
Root
Infinity
f(x)Function
ΔDelta
ΩOhm
λLambda
Nabla
Setting Up PostgreSQL and pgAdmin with Docker
Back to Blog
DevOpsDatabase

Setting Up PostgreSQL and pgAdmin with Docker

August 4, 2024

This post was previously on Medium

Introduction

Here's a detailed step-by-step guide to set up PostgreSQL and pgAdmin using Docker, including Docker network configuration and accessing the database using pgAdmin. This guide will ensure you have a fully functional database environment with a user-friendly interface for database management.

Article

Prerequisites: Make sure you have Docker installed on your computer. If not, download and install it from here: https://docs.docker.com/get-docker.

Steps:

1. Setting Up Docker Network

To ensure that the PostgreSQL and pgAdmin containers can communicate with each other, we need to connect them to the same Docker network.

  • Create a Docker Network: Run the following command in the terminal to create a new network named my-network:
docker network create my-network

2. Setting Up the PostgreSQL Container

  • Pull the PostgreSQL Docker Image: Download the latest PostgreSQL Docker image with the following command:
docker pull postgres
  • Run the PostgreSQL Container: Create and run a PostgreSQL container with the following command:
docker run --name my-postgres -e POSTGRES_PASSWORD=password --network my-network -d -p 5432:5432 postgres
  • --name my-postgres: Assigns the container the name "my-postgres".
  • -e POSTGRES_PASSWORD=password: Sets the password for the default postgres user.
  • --network my-network: Connects the container to the my-network network.
  • -d: Runs the container in the background.
  • -p 5432:5432: Maps port 5432 on the container to port 5432 on the host machine.

3. Setting Up the pgAdmin Container

  • Pull the pgAdmin Docker Image: Download the official pgAdmin Docker image with the following command:
docker pull dpage/pgadmin4
  • Run the pgAdmin Container: Create and run a pgAdmin container with the following command:
--name my-pgadmin
-p 5051:80
-v /path/to/local/directory:/var/lib/pgadmin
-e PGADMIN_DEFAULT_EMAIL=your_email@example.com
-e PGADMIN_DEFAULT_PASSWORD=your_password
--network my-network
-d dpage/pgadmin4

4. Creating a New Database on PostgreSQL

  • Check the PostgreSQL Container: Ensure the PostgreSQL container is running with the command:
docker ps
  • Access the PostgreSQL Container Shell: Enter the PostgreSQL container shell with:
docker exec -it my-postgres bash
  • Create a New Database: Inside the container shell, use psql to create a new database:
psql -U postgres CREATE DATABASE my_database;

5. Accessing the Database with pgAdmin

  • Check the pgAdmin Container: Ensure the pgAdmin container is running with the command:
docker ps
  • Access pgAdmin in Browser: Open your browser and access pgAdmin at http://localhost:5051 using the email and password you set earlier.
  • Create a New Server Connection in pgAdmin: Add a connection to the PostgreSQL server with the following details:
  • Host name/address: my-postgres (PostgreSQL container name).
  • Port: 5432.
  • Username: postgres.
  • Password: The password you set when running the PostgreSQL container.

Now, both containers (PostgreSQL and pgAdmin) should be able to communicate through the my-network network, and you can manage the PostgreSQL database using pgAdmin.

Comments