August 4, 2024
Muhammad Ikhwan Fathulloh
@ikhwan_fathullohProject Category:
This post was previously on Medium
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.
Prerequisites: Make sure you have Docker installed on your computer. If not, download and install it from here: https://docs.docker.com/get-docker.
To ensure that the PostgreSQL and pgAdmin containers can communicate with each other, we need to connect them to the same Docker network.
my-network
:docker network create my-network
docker pull postgres
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.docker pull dpage/pgadmin4
--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
docker ps
docker exec -it my-postgres bash
psql
to create a new database:psql -U postgres CREATE DATABASE my_database;
docker ps
my-postgres
(PostgreSQL container name).5432
.postgres
.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.
Reference: