This guide will help you set up PostgreSQL and pgAdmin using Docker and Docker Compose, and connect to PostgreSQL using pgAdmin.
- Docker installed on your machine
- Docker Compose installed on your machine
Create a docker-compose.yml file with the following content:
version: '3.1' services: db: image: postgres restart: always environment: POSTGRES_PASSWORD: mysecretpassword ports: - "5432:5432" volumes: - my_pgdata:/var/lib/postgresql/data container_name: my_postgres_container pgadmin: image: dpage/pgadmin4 restart: always environment: PGADMIN_DEFAULT_EMAIL: admin@admin.com PGADMIN_DEFAULT_PASSWORD: admin ports: - "5050:80" container_name: my_pgadmin_container volumes: my_pgdata:Open a terminal and navigate to the directory containing your Run the following command to start the services:
docker-compose up -dOpen your web browser and go to http://localhost:5050. Log in using the default credentials:
- Email:
admin@admin.com - Password:
admin
-
In pgAdmin, right-click on "Servers" in the left-hand pane and select "Create" > "Server...".
-
In the "Create - Server" dialog, fill in the following details:
General Tab:
- Name:
MyPostgresServer(or any name you prefer)
Connection Tab:
- Host name/address:
db(this is the service name defined indocker-compose.yml) - Port:
5432 - Maintenance database:
postgres - Username:
postgres - Password:
mysecretpassword
- Name:
-
Click "Save" to create the server connection. pgAdmin should now connect to your PostgreSQL instance.
To exit from the psql command-line interface, you can use one of the following commands:
-
Using the
\qCommand:\q
-
Using the
exitCommand:exit
-
Using the
Ctrl + DShortcut:- Press
Ctrl + Don your keyboard.
- Press
To shut down the PostgreSQL and pgAdmin services, run the following command:
docker-compose down-
Check Network Settings:
-
Ensure that the PostgreSQL container's port
5432is correctly mapped to the host's port5432ports: - "5432:5432"
-
-
Firewall and Security Groups:
- Ensure that your firewall or security groups allow connections to port
5432.
- Ensure that your firewall or security groups allow connections to port
-
Docker Network:
- If pgAdmin is running in a separate Docker container, ensure both containers are on the same Docker network. You can create a custom network in Docker Compose:
networks: my_network:
- If pgAdmin is running in a separate Docker container, ensure both containers are on the same Docker network. You can create a custom network in Docker Compose:
By following these steps, you should be able to run PostgreSQL using Docker and connect to it using pgAdmin.