第一行必须指令基于的基础镜像
From centos:7
复制pg_pathman文件夹到/tmp下
COPY pg_pathman /tmp/pg_pathman
COPY conf /tmp/conf
安装postgres + pglogical
RUN yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm &&
yum install -y postgresql96 postgresql96-server postgresql96-contrib postgresql96-devel && yum install -y gcc automake autoconf libtool make &&
yum install -y openssl openssl-devel
RUN yum install -y http://packages.2ndquadrant.com/pglogical/yum-repo-rpms/pglogical-rhel-1.0-3.noarch.rpm &&
yum install -y postgresql96-pglogical
ENV PATH /usr/pgsql-9.6/bin:$PATH
相当于cd
WORKDIR /tmp/pg_pathman
RUN make USE_PGXS=1 && make USE_PGXS=1 install
EXPOSE 5432
VOLUME [“/var/lib/pgsql/9.6/data/”]
CMD [“/usr/sbin/init”]
—================================
build dockerfile
1.dockerfile的文件名必须是Dockerfile。
2.创建文件夹dockerfile,解压pg_pathman.zip成pg_pathman文件夹,把pg_pathman文件夹和conf文件夹(postgresql.conf和pg_hba.conf)以及Dockerfile文件放到dockerfile文件夹中
- cd dockerfile
- docker build -t pg_pglogical_pathman_df . —- -t pg_pglogical_pathman_df是未来镜像的名字
- docker tag pg_pglogical_pathman_df:latest pg_pglogical_pathman_df:9.6
启动镜像
docker run -itd –privileged –name pg_pg_pa2 pg_pglogical_pathman_df:9.6 /usr/sbin/init — 创建并启动容器,没有端口暴露,只能使用命令行
docker exec -it pg_pg_pa2 /bin/bash –进入容器
启动容器后的操作
/usr/pgsql-9.6/bin/postgresql96-setup initdb — 初始化数据库
systemctl enable postgresql-9.6 –开机自启
cp /tmp/conf/postgresql.conf /var/lib/pgsql/9.6/data/postgresql.conf –修改配置文件
cp /tmp/conf/pg_hba.conf /var/lib/pgsql/9.6/data/pg_hba.conf –修改配置文件
systemctl start postgresql-9.6 — 启动服务
su – postgres
psql
alter user postgres with password ‘123456’; — 修改postgres超级用户密码为‘123456’;
创建数据库fintest,如果数据库已经存在则不需要
CREATE DATABASE fintest
WITH
OWNER = postgres
ENCODING = ‘UTF8’
LC_COLLATE = ‘en_US.utf8’
LC_CTYPE = ‘en_US.utf8’
TABLESPACE = pg_default
TEMPLATE = template0
CONNECTION LIMIT = -1;
添加扩展工具
\c fintest — 切换数据库到fintest
create extension pglogical; — create 扩展工具
create extension pg_pathman;
select extname, extowner from pg_extension; –查看扩展工具
测试pg_pathman工具
create table part_test(id int, info text, crt_time timestamp not null);
insert into part_test select id,md5(random()::text),clock_timestamp() from generate_series(1,100) t(id);
select * from part_test limit 10;– crt_time
select create_range_partitions(‘part_test’::regclass,’crt_time’,’2018-03-15 00:00:00′::timestamp,interval ‘1 month’,10,false);–创建分区修改’2018-03-15 00:00:00’为part_test的时间
select partition_table_concurrently(‘part_test’::regclass,10000,1.0);– 迁移数据到分区
select count(*) from only part_test; — 数据数量变成0,说明数据分区有效。
测试pglogical
这里的host,dbname,user,password需要根据实际使用的修改,需要同步的表,必须有主键
create table test(id serial primary key, name text); –创建测试表
alter table part_test add primary key(id);
*provider
select pglogical.create_node(node_name := ‘provider’,dsn :=’host=172.17.0.5 port=5432 dbname= fintest user= postgres password=123456′);
select pglogical.create_replication_set(‘mytest’); –create replication set 建变更集
select pglogical.replication_set_add_table(‘mytest’,’test’, true); —在变更集内添加表
- subscriber
select pglogical.create_node(node_name := ‘subscriber’,dsn :=’host=172.17.0.6 port=5432 dbname=fintest user=postgres password=123456′);
select pglogical.create_subscription(subscription_name :=’subscription’, provider_dsn := ‘host=172.17.0.5 port =5432 dbname=fintest user=postgres password=123456’,replication_sets := ‘{mytest}’ ); –对于自己建的变更集如何设置订阅
验证同步有效行
provider
insert into test(name) values (‘aa’);
insert into test(name) values (‘bb’);
select * from test; –2
subscriber
select * from test; –2