一、拉取Postgresql镜像文件
[root@m1 ~]# docker pull postgres:latest
二、创建挂载目录并授权
[root@m1 ~]# mkdir -p /data/pgdata
三、创建并启动Postgresql容器
[root@m1 ~]# docker run \
-itd \
-p 5432:5432 \
-v /data/pgdata:/var/lib/postgresql/data \
-v /etc/localtime:/etc/localtime \
-e POSTGRES_PASSWORD=123456 \
--restart always \
--privileged=true \
--name my_postgres \
postgres:latest
四、查看Postgresql是否启动成功
[root@m1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2a1681b11a9 postgres:latest "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp my_postgres
五、查看docker容器日志
[root@m1 ~]# docker logs my_postgres
PostgreSQL Database directory appears to contain a database; Skipping initialization
2022-01-24 14:53:00.365 CST [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-01-24 14:53:00.365 CST [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-01-24 14:53:00.365 CST [1] LOG: listening on IPv6 address "::", port 5432
2022-01-24 14:53:00.368 CST [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-01-24 14:53:00.374 CST [27] LOG: database system was interrupted; last known up at 2022-01-24 14:48:27 CST
2022-01-24 14:53:00.390 CST [27] LOG: database system was not properly shut down; automatic recovery in progress
2022-01-24 14:53:00.392 CST [27] LOG: redo starts at 0/16FAB48
2022-01-24 14:53:00.392 CST [27] LOG: invalid record length at 0/16FAB80: wanted 24, got 0
2022-01-24 14:53:00.393 CST [27] LOG: redo done at 0/16FAB48 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2022-01-24 14:53:00.405 CST [1] LOG: database system is ready to accept connections
六、拉取pgadmin4工具镜像
[root@m1 ~]# docker pull dpage/pgadmin4:latest
七、创建并启动pgadmin4容器
[root@m1 ~]# docker run \
-itd \
-p 5080:80 \
-v /etc/localtime:/etc/localtime \
-e PGADMIN_DEFAULT_EMAIL=hui.jin@dataxgroup.com \
-e PGADMIN_DEFAULT_PASSWORD=123456 \
--restart always \
--privileged=true \
--name pgadmin \
dpage/pgadmin4
八、查看pgadmin4是否启动成功
[root@m1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eed7a36f995e dpage/pgadmin4 "/entrypoint.sh" 7 seconds ago Up 4 seconds 443/tcp, 0.0.0.0:5080->80/tcp, :::5080->80/tcp pgadmin
a2a1681b11a9 postgres:latest "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp my_postgres
九、验证连接
#1.登录pgadmin4,输入启动容器时的邮箱和密码即可
#2.创建新的service连接,连接host:192.168.13.200,默认的用户名为postgres,点击保存即可看到连接成功。