docker安装 PostgreSQL

时间:2021-08-12
本文章向大家介绍docker安装 PostgreSQL,主要包括docker安装 PostgreSQL使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一)安装

1、安装 PostgreSQL
[root@localhost ~]# docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
33847f680f63: Pull complete 
1b09e96014b3: Pull complete 
eb49b6d9d1f3: Pull complete 
4057ebf78d2d: Pull complete 
f92d870e2c4f: Pull complete 
b03847575a18: Pull complete 
475945131fa9: Pull complete 
c042b5a6607d: Pull complete 
cfe883b776dc: Pull complete 
61af04e5c3eb: Pull complete 
4e9965ae9062: Pull complete 
7b9708b81aa6: Pull complete 
871877336770: Pull complete 
Digest: sha256:6647385dd9ae11aa2216bf55c54d126b0a85637b3cf4039ef24e3234113588e3
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

#检查是否安装成功
[root@localhost ~]# docker images 
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
postgres          latest    b2fcd079c1d4   2 weeks ago    315MB
jenkins/jenkins   latest    171f17febd29   4 months ago   572MB
[root@localhost ~]# docker images | grep postgres
postgres          latest    b2fcd079c1d4   2 weeks ago    315MB

2.创建容器

docker的容器默认情况下只能由本地主机访问,即A主机上的容器不能被B主机访问,所以要做端口映射

[root@localhost ~]# docker run --name postgres -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d postgres
e238f591a836031104964688110cd49afb2679adc94c69aeab444f5c5ce91bee

解释:

run :创建并运行一个容器

--name :指定创建的容器的名字

-e POSTGRES_PASSWORD=password,设置环境变量,指定数据库的登录口令是password;

p 54321:5432,端口映射将容器的5432端口映射到外部机器的54321端口;

-d postgres,指定使用 postgres 作为镜像

2.1验证结果
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS                        PORTS                                                  NAMES
e238f591a836   postgres          "docker-entrypoint.s…"   9 seconds ago   Up 7 seconds                  0.0.0.0:5432->5432/tcp, :::5432->5432/tcp              postgres
3a0c5a912f71   jenkins/jenkins   "/sbin/tini -- /usr/…"   3 months ago    Exited (255) 45 minutes ago   50000/tcp, 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp   jenkins1
3、连接数据库,进入psql命令行
[root@localhost ~]# docker exec -it postgres /bin/bash
root@e238f591a836:/# psql -U postgres -W
Password: 
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.


原文地址:https://www.cnblogs.com/ye-peng/p/15134399.html