Docker与PostgreSQL 11.5系列文章(二)PostgreSQL 11.5安装

者简介:
赵振平,PostgreSQL中文社区主席、计算机畅销书作家、贵州省省管专家、太阳塔科技CTO、国家首批大数据高级职称、腾讯最具价值专家(TVP)、电子工业出版社签约作家、出版了技术专著《Oracle数据库精讲与疑难解析》、出版了技术专著《成功之路:Oracle 11g学习笔记》、出版了技术专著《IT架构实录》。

背景介绍
上篇介绍容器相关的基本概念,介绍如何安装docker。如果不熟悉docker的安装,可回到系列文章的第一部分《Docker与PostgreSQL 11.5系列文章(一)Docker的安装》。(请点击文章底部“阅读原文”查看)第二部分主要介绍PostgreSQL11在容器中的安装和使用。

下载镜像
这里下载的postgreSQL 11.5版本的镜像
[root@tar1 yum.repos.d]# docker pull postgres:11.5
11.5: Pulling from library/postgres
9fc222b64b0a: Pull complete
38296355136d: Pull complete
2809e135bbdb: Pull complete
77907400401d: Pull complete
1f8ceea7d21e: Pull complete
1ede1f2cb1b8: Pull complete
dcfbc2751885: Pull complete
8379f81b7622: Pull complete
08034f3aaedc: Pull complete
cb6db97b313d: Pull complete
7a41d1ed6f3b: Pull complete
15f5a1254505: Pull complete
5a9451e12286: Pull complete
55bb5e2d55ba: Pull complete
Digest: sha256:f766d03bb49c7dd16fe32c1eb2645d13cb315adc029557cd1e4a0a9c094933d5
Status: Downloaded newer image for postgres:11.5
下载要花费一定的时间。

查看镜像
[root@tar1 yum.repos.d]# docker images |grep post
postgres 11.5 c3fe76fef0a6 9 days ago 312MB
[root@tar1 yum.repos.d]#
下载成功以后,可以查看镜像的信息(包括大小)。

创建和运行容器
[root@tar1 yum.repos.d]# docker run --name dockerPG11 -e POSTGRES_PASSWORD=Xzzp2008 -p 54322:5432 -d postgres:11.5
ccb3d0bb1a303e081fac20622c0b821b71720308280a8d1a55b906e792fe6452

  1. run 创建并运行一个容器(实例);
  2. –name 指定创建的容器的名字;
  3. -e POSTGRES_PASSWORD= Xzzp2008 设置环境变量,指定数据库的登录口令为Xzzp2008;
  4. -p 54322:5432 端口映射将容器的5432端口映射到外部机器的54322端口,从外部访问就使用这个端口;
  5. -d postgres:11.5 镜像的名字是postgres:11.5,镜像的名字是查询出来的,不是由用户随意定的。

查看实例
[root@tar1 yum.repos.d]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccb3d0bb1a30 postgres:11.5 “docker-entrypoint…” 37 seconds ago Up 36 seconds 5432/tcp, 0.0.0.0:54322->54333/tcp dockerPG11
可以看到实例运行的时间,ID号等信息。

多了参数-a ,是不一样的输出
[root@tar1 yum.repos.d]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ccb3d0bb1a30 postgres:11.5 “docker-entrypoint…” 14 minutes ago Up 14 minutes 5432/tcp, 0.0.0.0:54322->54333/tcp dockerPG11
3b8d602037b4 hello-world “/hello” About an hour ago Exited (0) About an hour ago jolly_wright
c5cdfc83f7bd hello-world “/hello” About an hour ago Exited (0) About an hour ago elegant_edison

查看日志
[root@tar1yum.repos.d]# docker logs ccb3d0bb1a303e081fac20622c0b821b71720308280a8d1a55b906e792fe6452
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale “en_US.utf8”.
The default database encoding has accordingly been set to “UTF8”.
The default text search configuration will be set to “english”.

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 128MB
selecting default timezone … Etc/UTC
selecting dynamic shared memory implementation … posix
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start

WARNING: enabling “trust” authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and --auth-host, the next time you run initdb.
waiting for server to start…2019-08-23 09:48:02.765 UTC [39] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
2019-08-23 09:48:02.791 UTC [40] LOG: database system was shut down at 2019-08-23 09:48:01 UTC
2019-08-23 09:48:02.797 UTC [39] LOG: database system is ready to accept connections
done
server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down…2019-08-23 09:48:02.846 UTC [39] LOG: received fast shutdown request
2019-08-23 09:48:02.849 UTC [39] LOG: aborting any active transactions
2019-08-23 09:48:02.852 UTC [39] LOG: background worker “logical replication launcher” (PID 46) exited with exit code 1
2019-08-23 09:48:02.852 UTC [41] LOG: shutting down
2019-08-23 09:48:02.881 UTC [39] LOG: database system is shut down
done
server stopped

PostgreSQL init process complete; ready for start up.

2019-08-23 09:48:02.967 UTC [1] LOG: listening on IPv4 address “0.0.0.0”, port 5432
2019-08-23 09:48:02.967 UTC [1] LOG: listening on IPv6 address “::”, port 5432
2019-08-23 09:48:02.973 UTC [1] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
2019-08-23 09:48:02.989 UTC [48] LOG: database system was shut down at 2019-08-23 09:48:02 UTC
2019-08-23 09:48:02.996 UTC [1] LOG: database system is ready to accept connections

进入容器里面(进入容器内部)
运行中的容器其实是一个功能完备的Linux操作系统,所以我们可以像常规的系统一样登录并访问容器。
[root@tar1 yum.repos.d]# docker exec -it dockerPG11 bash
root@ccb3d0bb1a30:/#
dockerPG11是容器的名字。
ccb3d0bb1a30是容器的ID号。

查看PG进程(在容器里)
root@ccb3d0bb1a30:/# ps -ef |grep post
postgres 1 0 0 09:47 ? 00:00:00 postgres
postgres 49 1 0 09:48 ? 00:00:00 postgres: checkpointer
postgres 50 1 0 09:48 ? 00:00:00 postgres: background writer
postgres 51 1 0 09:48 ? 00:00:00 postgres: walwriter
postgres 52 1 0 09:48 ? 00:00:00 postgres: autovacuum launcher
postgres 53 1 0 09:48 ? 00:00:00 postgres: stats collector
postgres 54 1 0 09:48 ? 00:00:00 postgres: logical replication launcher
root 69 55 0 10:10 ? 00:00:00 grep post
注意:看到ID号ccb3d0bb1a30,你就知道,当前在容器内部。

切换到OS用户postgres(在容器里)
root@ccb3d0bb1a30:/# su - postgres
postgres@ccb3d0bb1a30:~$
postgres@ccb3d0bb1a30:~$
登录数据库(在容器里)
postgres@ccb3d0bb1a30:~$ psql -d postgres
psql (11.5 (Debian 11.5-1.pgdg90+1))
Type “help” for help.

postgres=#

查看映射到docker外的端口
[root@tar1 ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp6 0 0 ::1:25 ::? LISTEN
tcp6 0 0 :::111 ::? LISTEN
tcp6 0 0 :::54322 ::? LISTEN
tcp6 0 0 :::22 ::? LISTEN
tcp6 0 0 ::1:631
54321就是访问容器内postgres数据库的端口号。

登录数据库(从容器外部)
从宿主机登录容器数据库
-bash-4.2$ psql -U postgres -h localhost -p 54322
用户 postgres 的口令:
psql (11.5)
输入 “help” 来获取帮助信息.

postgres=#
查看版本号
postgres=# select version();
version
----------------------------------------------------------------------------------
PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64
-bit
(1 行记录)

postgres=#
54322是映射以后的密码,

DML测试
创建表,插入数据,查询数据等一系列实验:
postgres=# CREATE TABLE weather (
postgres(# city varchar(80),
postgres(# temp_lo int, – low temperature
postgres(# temp_hi int, – high temperature
postgres(# prcp real, – precipitation
postgres(# date date
postgres(# );
CREATE TABLE
postgres=# INSERT INTO weather VALUES (‘San Francisco’,46,50,0.25, ‘1994-11-27’);
INSERT 0 1
postgres=# INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES (‘San Francisco’, 43, 57, 0.0, ‘1994-11-29’);
INSERT 0 1
postgres=# INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES (‘1994-11-29’, ‘Hayward’, 54, 37);
INSERT 0 1
postgres=# SELECT * FROM weather;
city | temp_lo | temp_hi | prcp | date
---------------±--------±--------±-----±-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 43 | 57 | 0 | 1994-11-29
Hayward | 37 | 54 | | 1994-11-29
(3 行记录)

postgres=#

postgres=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------±--------±-------±---------
public | weather | 数据表 | postgres
(1 行记录)
停止实例(容器)
docker stop ccb3d0bb1a30
ccb3d0bb1a30是实例(容器)的ID
或者(dockerPG11是容器的名字)
docker stop dockerPG11
其实也是停止PG11.5。

删除容器
[root@tar1 ~]# docker rm dockerPG11
dockerPG11
dockerPG11是容器的名字
删除容器以后,可以把容器所引用的卷给删除
[root@tar1 ~]# ll /var/lib/docker/volumes/
总用量 32
drwxr-xr-x. 3 root root 19 8月 23 23:46 cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f
-rw-------. 1 root root 65536 8月 25 12:13 metadata.db
[root@tar1 ~]# docker volume rm cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f
cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f
[root@tar1 ~]# ll /var/lib/docker/volumes/
总用量 32
-rw-------. 1 root root 65536 8月 25 12:32 metadata.db
[root@tar1 ~]#

总结
在这篇文章的学习中,需注意端口是如何映射的,还要特别注意在容器里面和容器外面看到的情况。同时注意: 如何从容器外登录数据库?如何从容器里登录数据库?停止docker服务和停止docker的区别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值