The Easy Way to Setup PostgreSQL 10 Logical Replication
2017-06-15 00:10
399 查看
按:转载一篇介绍PostgreSQL 10.0的逻辑复制特性使用方法的文章,逻辑复制特性的实现机制稍后有时间分析。
原文地址:https://www.openscg.com/2017/06/the-easy-way-to-setup-postgresql-10-logical-replication/
One of the most exciting enhancements that comes with the new PostgreSQL 10 release, is Logical Replication. The functionality, although not yet as extensive as pgLogical which
it is based on, provides a powerful replication option when you want control over table backups at a finite level – allowing all kinds of ETL goodness like:
replicating only certain tables, columns, or rows
consolidating multiple databases into a single one
sharing a subset of the database between multiple databases
replicating between different major versions of PostgreSQL
You want to try it but can’t face installation hell? The BigSQL distribution has your back.
We make it easy.
Make 2 directories:
Navigate to your pub_test directory:
Install the a postgresql 10 instance (Windows users don’t prefix the pgc command with ./ as shown in the following commands):
Repeat all of the commands in step 3 after navigating to your sub_test directory:
The first installation, pub_test, will default to port 5432.
The second installation, sub_test, will default to port 5433.
For logical replication to work, you will first need to change the configuration parameter
So, you will need to edit the file postgresql.conf for BOTH of your PostgreSQL (pub_test AND sub_test). Navigate to the directory where postgresql.conf is located:
Open the file with your favorite editor and change
Navigate to the bigsql directory and restart pg10:
For this exercise, we will create a rock legends database and setup logical replication on the rocker girls table.
Connect as a superuser to the PostgreSQL instance that will serve as the publisher:
Create rock_legends_db database:
Create table rocker_girls:
Add record to rocker_girls table:
Check to see that rocker_girls table with data was created:
Setup the database as the publisher for the rocker_girls table:
Exit psql:
Connect as a superuser to the PostgreSQL instance that will serve as the subscriber:
Repeat steps 2 – 3 from above. DO NOT add any records to the table!
Setup the database as the subscriber:
Verify that the rocker_girls “Debbie Harry” row was replicated:
Debbie Harry has been replicated!
Connect to the publisher:
Add a new record:
Disconnect from publisher and connect to subscriber:
Run select all on rocker_girls table:
Janis has been replicated!
In upcoming posts, I’ll show more ways to manage your replications. In the meantime, you can continue to test in your sandboxes. And in case you need to blow away your logical replication …
By Holly Orr| June
7th, 2017
原文地址:https://www.openscg.com/2017/06/the-easy-way-to-setup-postgresql-10-logical-replication/
One of the most exciting enhancements that comes with the new PostgreSQL 10 release, is Logical Replication. The functionality, although not yet as extensive as pgLogical which
it is based on, provides a powerful replication option when you want control over table backups at a finite level – allowing all kinds of ETL goodness like:
replicating only certain tables, columns, or rows
consolidating multiple databases into a single one
sharing a subset of the database between multiple databases
replicating between different major versions of PostgreSQL
Want to try it without the fuss?
Installing multiple PostgreSQL 10 sandboxes on your localhost in 4 easy steps
You want to try it but can’t face installation hell? The BigSQL distribution has your back.We make it easy.
Make 2 directories:
# the logical replication publisher mkdir pub_test # the logical replication subscriber mkdir sub_test
Navigate to your pub_test directory:
cd pub_test
Install the a postgresql 10 instance (Windows users don’t prefix the pgc command with ./ as shown in the following commands):
# use this for MAC / Linux: python -c "$(curl -fsSL http://s3.amazonaws.com/pgcentral/install.py)" # use this for Windows: @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://s3.amazonaws.com/pgcentral/install.ps1'))" # run pgc commands to install pg10 cd bigsql ./pgc install pg10 ./pgc start pg10 # make sure the instance is running ./pgc status
Repeat all of the commands in step 3 after navigating to your sub_test directory:
cd sub_test
Take note that:
The first installation, pub_test, will default to port 5432.The second installation, sub_test, will default to port 5433.
Edit postgresql.conf
For logical replication to work, you will first need to change the configuration parameter wal_levelto ‘logical’.
So, you will need to edit the file postgresql.conf for BOTH of your PostgreSQL (pub_test AND sub_test). Navigate to the directory where postgresql.conf is located:
cd {pub_test,sub_test}/bigsql/data/pg10
Open the file with your favorite editor and change
wal_levelfrom ‘hot_standby’ to ‘logical’
#------------------------------------------------------------------------------ # WRITE AHEAD LOG #------------------------------------------------------------------------------ # - Settings - wal_level = logical # (change requires restart)
Navigate to the bigsql directory and restart pg10:
cd ../.. ./pgc restart pg10
Exercise: Rock Legends Database
For this exercise, we will create a rock legends database and setup logical replication on the rocker girls table.
Create the publisher
Connect as a superuser to the PostgreSQL instance that will serve as the publisher:
#publisher instance in running on port 5432 - the default psql -h localhost -p 5432 -U postgres
Create rock_legends_db database:
postgres=# CREATE DATABASE rock_legends_db; postgres=# \c rock_legends_db; You are now connected to database "rock_legends_db" as user "postgres".
Create table rocker_girls:
CREATE TABLE rocker_girls ( name text PRIMARY KEY, band text NOT NULL, record_creation_date timestamptz NOT NULL DEFAULT now() );
Add record to rocker_girls table:
INSERT INTO rocker_girls(name, band) VALUES('Debbie Harry', 'Blondie');
Check to see that rocker_girls table with data was created:
rock_legends_db=# select * from rocker_girls; name | band | record_creation_date ------------ | ------- | ----------------------------- Debbie Harry | Blondie | 2017-05-30 12:52:12.177873-04 (1 row)
Setup the database as the publisher for the rocker_girls table:
CREATE PUBLICATION pub_rock FOR TABLE rocker_girls;
Exit psql:
rock_legends_db=# \q
Create the subscriber
Connect as a superuser to the PostgreSQL instance that will serve as the subscriber:
#subscriber instance in running on port 5433 - the default psql -h localhost -p 5433 -U postgres
Repeat steps 2 – 3 from above. DO NOT add any records to the table!
Setup the database as the subscriber:
CREATE SUBSCRIPTION sub_rock CONNECTION 'host=localhost port=5432 dbname=rock_legends_db' PUBLICATION pub_rock;
Verify that the rocker_girls “Debbie Harry” row was replicated:
rock_legends_db=# select * from rocker_girls; name | band | record_creation_date ------------ | ------- | ----------------------------- Debbie Harry | Blondie | 2017-05-30 12:52:12.177873-04 (1 row)
Debbie Harry has been replicated!
Test the replication by adding another record
Connect to the publisher:psql -h localhost -p 5432 -U postgres -d rock_legends_db
Add a new record:
INSERT INTO rocker_girls(name, band) VALUES('Janis Joplin', 'Big Brother and the Holding Company');
Disconnect from publisher and connect to subscriber:
rock_legends_db=# \q psql -h localhost -p 5433 -U postgres -d rock_legends_db
Run select all on rocker_girls table:
rock_legends_db=# select * from rocker_girls; name | band | record_creation_date ------------ | ----------------------------------- | ----------------------------- Debbie Harry | Blondie | 2017-05-30 12:52:12.177873-04 Janis Joplin | Big Brother and the Holding Company | 2017-05-31 08:22:57.708333-04 (2 rows)
Janis has been replicated!
What’s next
In upcoming posts, I’ll show more ways to manage your replications. In the meantime, you can continue to test in your sandboxes. And in case you need to blow away your logical replication …
How to delete the subscriber and publisher
rock_legends_db=# drop subscription testsub; NOTICE: dropped replication slot "rock_legends_db" on publisher DROP SUBSCRIPTION rock_legends_db=# drop publication alltables; DROP PUBLICATION
By Holly Orr| June
7th, 2017
相关文章推荐
- Easy way to estimate a table size in the future
- The Easy Way to Extract Useful Text from Arbitrary HTML 从HTML文件中抽取正文的简单方案
- http://ai-depot.com/articles/the-easy-way-to-extract-useful-text-from-arbitrary-html/
- Porting Over iPad game to iphone the easy way cocos2d
- An easy way to setup a screaming server on Windows
- The Easy Way To Get Recent Comments In WordPress
- The easy way to execute sudo command in Python using subprocess.Popen
- The Easy Way to Extract Useful Text from Arbitrary HTML
- Porting Over iPad game to iphone the easy way cocos2d
- The Easy Way to Extract Useful Text from Arbitrary HTML
- Easy way to install the bluetooth(BCM2045A)
- The easy way to run Powershell 2.0 using .NET Framework 4.0
- 10 Easy Ways To Protect Your Eyes From The Harms of Digital Devices
- The way to learn English
- How To Compile A Kernel - The Ubuntu Way
- [howto] General 5.10 - HowTo: Speed up ubuntu boot process - the way you can feel it. - updated
- An easy way to change colour in text control
- Visual C the best way to get the contents of a file directory into a list
- What is the best way to create a good interface to an abstraction?
- A cool tool,give you a easy way to deploy you .net app