在 docker 中快速使用各种版本的 PostgreSQL 数据库(9.4、9.6、10、11、12、13、14、15 等)。
最编程
2024-04-14 11:20:29
...
1、安装概述
PG安装方法很多,和MySQL类似,给用户提供很大的选择空间。如:RPM包安装(在线、离线)、源码编译安装、系统自带、二进制、NDB安装等。
官网:https://www.postgresql.org/
rpm包:https://yum.postgresql.org/rpmchart.php
yum源:https://yum.postgresql.org/repopackages.php
源码包:https://www.postgresql.org/ftp/source/
打开 PostgreSQL 官网 https://www.postgresql.org/,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 Linux、Windows、Mac OS等 。
2、Docker中快速安装部署各个版本的PG环境
Docker Hub的官网地址:https://hub.docker.com/_/postgres
GitHub的地址:https://github.com/docker-library/postgres
-- 拉取所有镜像
nohup docker pull postgres:9.4.26 &
nohup docker pull postgres:9.6.24 &
nohup docker pull postgres:10.21 &
nohup docker pull postgres:11.16 &
nohup docker pull postgres:12.16 &
nohup docker pull postgres:13.12 &
nohup docker pull postgres:14.9 &
nohup docker pull postgres:15.4 &
cat << EOF > /tmp/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all ::1/128 trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 md5
EOF
docker rm -f lhrpg94 lhrpg96 lhrpg10 lhrpg11 lhrpg12 lhrpg13 lhrpg14
docker run --name lhrpg94 -h lhrpg94 -d -p 54321:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:9.4.26
docker run --name lhrpg96 -h lhrpg96 -d -p 54322:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:9.6.24
docker run --name lhrpg10 -h lhrpg10 -d -p 54323:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:10.21
docker run --name lhrpg11 -h lhrpg11 -d -p 54324:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:11.16
docker run --name lhrpg12 -h lhrpg12 -d -p 54325:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:12.16
docker run --name lhrpg13 -h lhrpg13 -d -p 54326:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:13.12
docker run --name lhrpg14 -h lhrpg14 -d -p 54327:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:14.9
docker run --name lhrpg15 -h lhrpg15 -d -p 54328:5432 -e POSTGRES_PASSWORD=lhr -e TZ=Asia/Shanghai postgres:15.4
docker exec -it lhrpg12 bash
docker exec -it lhrpg12 psql -U postgres -d postgres
select * from pg_tables;
select version();
psql -U postgres -h 172.17.0.12 -d postgres
psql -U postgres -h 192.168.8.8 -p 54324 -d postgres -W
docker run -p 54321:5432 -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=lhr -e TZ=PRC -d --name=lhrpg11 postgres:11.5
-- Debian中的PG
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
apt-get update -y
sudo apt-get -y install postgresql
3、登陆测试
-- docker直接登陆
docker exec -it lhrpg14 psql -U postgres -d postgres
-- 本地登陆
docker exec -it lhrpg14 bash
su - postgres
psql
-- 远程登陆
psql -U postgres -h 192.168.66.35 -d postgres -p54327
-- 从Postgresql 9.2开始,还可以使用URI格式进行远程连接:psql postgresql://myuser:mypasswd@myhost:5432/mydb
psql postgresql://postgres:lhr@192.168.66.35:54327/postgres
其中-h参数指定服务器地址,默认为127.0.0.1,默认不指定即可,-d指定连接之后选中的数据库,默认也是postgres,-U指定用户,默认是当前用户,-p 指定端口号,默认是"5432",其它更多的参数选项可以执行:./bin/psql --help 查看。
C:\Users\lhrxxt>psql -U postgres -h 192.168.66.35 -d postgres -p54327
Password for user postgres:
psql (13.3)
Type "help" for help.
postgres=# select version();
version
------------------------------------------------------------------------------------------------------------------
PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
(1 row)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------------+-------------------+-----------------------
postgres | postgres | UTF8 | Chinese_China.936 | Chinese_China.936 |
template0 | postgres | UTF8 | Chinese_China.936 | Chinese_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese_China.936 | Chinese_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# CREATE DATABASE lhrdb WITH OWNER=postgres ENCODING='UTF-8';
CREATE DATABASE
postgres=# \c lhrdb
You are now connected to database "lhrdb" as user "postgres".
lhrdb=#
lhrdb=# create table student (
lhrdb(# id integer not null,
lhrdb(# name character(32),
lhrdb(# number char(5),
lhrdb(# constraint student_pkey primary key (id)
lhrdb(# );
CREATE TABLE
lhrdb=#
lhrdb=# \d student
Table "public.student"
Column | Type | Collation | Nullable | Default
--------+---------------+-----------+----------+---------
id | integer | | not null |
name | character(32) | | |
number | character(5) | | |
Indexes:
"student_pkey" PRIMARY KEY, btree (id)
lhrdb=#
lhrdb=# INSERT INTO student (id, name, number) VALUES (1, '张三', '1023');
INSERT 0 1
lhrdb=# SELECT * FROM student WHERE id=1;
id | name | number
----+------------------------------------+--------
1 | 张三 | 1023
(1 row)
是不是很方便呢。
麦老师自制PG环境汇总
麦老师的镜像中包括了PG 9.4、9.6、10、11、12、13、14、15各个版本,都是采用源码安装,可以直接使用,满足各类测试要求:
docker rm -f lhrpgall
docker run -itd --name lhrpgall -h lhrpgall \
-p 25432-25445:5432-5445 -p 122:22 -p 189:3389 \
-v /sys/fs/cgroup:/sys/fs/cgroup \
--restart=always \
--privileged=true lhrbest/lhrpgall:3.0 \
/usr/sbin/init
docker exec -it lhrpgall bash
systemctl status pg94 pg96 pg10 pg11 pg12 pg13 pg14 pg15
systemctl status postgresql-13.service
[root@lhrpgall /]# ps -ef|grep postgres | grep bin
pg15 229 1 0 12:11 ? 00:00:00 /pg15/pg15/bin/postgres -D /pg15/pgdata -p 5440
pg10 231 1 0 12:11 ? 00:00:00 /pg10/pg10/bin/postgres -D /pg10/pgdata -p 5436
pg13 232 1 0 12:11 ? 00:00:00 /pg13/pg13/bin/postgres -D /pg13/pgdata -p 5433
pg14 235 1 0 12:11 ? 00:00:00 /pg14/pg14/bin/postgres -D /pg14/pgdata -p 5439
pg94 243 1 0 12:11 ? 00:00:00 /pg94/pg94/bin/postgres -D /pg94/pgdata -p 5438
pg11 244 1 0 12:11 ? 00:00:00 /pg11/pg11/bin/postgres -D /pg11/pgdata -p 5435
pg96 247 1 0 12:11 ? 00:00:00 /pg96/pg96/bin/postgres -D /pg96/pgdata -p 5437
pg12 249 1 0 12:11 ? 00:00:00 /pg12/pg12/bin/postgres -D /pg12/pgdata -p 5434
安装配置完成,若有不懂,可以私聊麦老师。