Simple Steps#
-
Install Docker, you can refer to Installing Docker and Basic Usage or search on Baidu for solutions.
-
Pull the Docker image file for PostgreSQL:
docker pull postgres
- Create a Docker volume named "dv_pgdata" (actually, manual creation can be omitted, you can proceed to the next step and Docker will create it automatically):
docker volume create dv_pgdata
- Start the container, use -v to specify mapping the data directory of postgres to the dv_pgdata created above:
docker run --name my_postgres -v dv_pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=xxxxxx -p 5432:5432 -d postgres
- Now check the existing Docker volumes:
docker volume ls
- View volume information:
cn2d6@navxin-desktop:~$ docker inspect dv_pgdata
- On the host machine, you can also directly view the contents of the volume:
cn2d6@navxin-desktop:~$ cd /var/lib/docker/volumes/dv_pgdata/_data
cn2d6@navxin-desktop:~$ ll
- View PostgreSQL:
cn2d6@navxin-desktop:~$docker exec -it 618 bash
root@618f1a4128ee:/# psql -U postgres -d postgres -p 5432 -h 127.0.0.1
- In many cases, we hope to use a graphical interface to manage and operate the database. You can deploy the pgadmin tool (for example below), and then access the host's 5080 port in the browser to open pgadmin.
docker pull dpage/pgadmin4
docker run --name pgadmin -p 5080:80 \
-e '[email protected]' \
-e 'PGADMIN_DEFAULT_PASSWORD=xxxxxx' \
-e 'PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION=True' \
-e 'PGADMIN_CONFIG_LOGIN_BANNER="Authorised users only!"' \
-e 'PGADMIN_CONFIG_CONSOLE_LOG_LEVEL=10' \
-d dpage/pgadmin4
Deploying PostgreSQL and pgAdmin4 with docker-compose#
Preparation#
Install docker-compose
apt install docker-compose
Make sure you have pulled the postgres and pgAdmin4 images, if not done:
docker pull postgres
docker pull dpage/pgadmin4
Configuration#
Then create a file named docker-compose.yml in any directory:
touch ./docker-compose.yml
The content is as follows:
# Use postgres/example user/password credentials
# https://hub.docker.com/_/postgres?tab=description
# Run in the current directory: sudo docker-compose up -d
# To stop running, run in the current directory: sudo docker-compose down
# View docker routing address: sudo docker inspect postgres_baicai
# sudo docker kill $(sudo docker ps -aq)
# sudo docker rm $(sudo docker ps -aq)
version: '3.1'
services:
db:
image: postgres
restart: always
privileged: true
container_name: postgres_baicai
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: YourPassword
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- /navxin/kn1/baicai_docker/baicai_postgres/pg_data:/var/lib/postgresql/data
# - pgdata:/var/lib/postgresql/data
pgadmin4:
image: dpage/pgadmin4
restart: always
container_name: pgadmin_baicai
ports:
- 5080:80
environment:
PGADMIN_DEFAULT_EMAIL: "[email protected]"
PGADMIN_DEFAULT_PASSWORD: YourPassword
# volumes:
# pgdata:
Deployment#
Run in the current directory:
docker-compose up -d
To stop running, run in the current directory:
docker-compose down
Connection Steps
Open a browser and enter localhost:5080. After logging in to pgAdmin4, click on "Add New Server". Pay special attention, the connection address IP should be filled with the docker routing address, and the port should be filled with 5432.
Method to view docker routing address
docker inspect postgres_baicai
In the output content, find Gateway, the corresponding address is the docker routing address.