2003-08-04 Dobrica Pavlinusic <dpavlin@rot13.org>
This HOWTO is based on Setting up RServ with PostgreSQL but with replication to remote host with ssh security. It's written for Debian GNU/Linux, so if you are using something else, you might have to adjust paths to commands.
You should also note that this HOWTO uses RServ utilities from RServ improved by community which is special version improved by Nélio Alves Pereira Filho and myself. However, special care has been taken to make RServ 0.3 work with normal RServ 0.1 PostgreSQL module so that you can just install postgresql-contrib package and just perl scripts from RServ 0.3 (this makes upgrades easy). You will have to do this only on master host. Slave host doesn't need postgresql-contrib package or RServ installation.
Bare in mind that RServ in PostgreSQL contrib, is version 0.1. While I haven't had any problems using it, PostgreSQL, Inc. is actively working on eRServer with more features, including HA.
Let's first give a rough overview of parameters used in this HOWTO:
Database name: rep
Remote host: luna
Remote user and password for RServ replication: rserv and BATyLAaf
dpavlin@llin:~/rserv$ createdb rep CREATE DATABASE dpavlin@llin:~/rserv$ psql < misc/rep-demo.sql rep ERROR: table "a_b" does not exist ERROR: table "a" does not exist ERROR: table "b" does not exist ERROR: table "c" does not exist NOTICE: CREATE TABLE will create implicit sequence 'a_id_seq' for SERIAL column 'a.id' CREATE TABLE CREATE INDEX NOTICE: CREATE TABLE will create implicit sequence 'b_id_seq' for SERIAL column 'b.id' CREATE TABLE CREATE INDEX NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) CREATE TABLE NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'c_pkey' for table 'c' CREATE TABLE
dpavlin@llin:~/rserv$ pg_dump rep > /tmp/rep.sql
dpavlin@llin:~/rserv$ ssh luna createdb rep CREATE DATABASE dpavlin@llin:~/rserv$ ssh luna psql rep < /tmp/rep.sql ...You might run into problems if you try to reload pg_dump from higher PostgreSQL version to lower (e.g. from 7.3.x to 7.2.x).
dpavlin@llin:~/rserv$ ./bin/MasterInit rep NOTICE: CREATE TABLE will create implicit sequence '_rserv_servers__server_seq' for SERIAL column '_rserv_servers_.server' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index '_rserv_servers__pkey' for table '_rserv_servers_' NOTICE: CREATE TABLE / UNIQUE will create implicit index '_rserv_servers__host_key' for table '_rserv_servers_' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index '_rserv_tables__pkey' for table '_rserv_tables_' NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
dpavlin@llin:~/rserv$ ssh luna "psql -c \"create user rserv with encrypted password 'BATyLAaf' nocreatedb nocreateuser\" rep" CREATE USER
# TYPE DATABASE IP_ADDRESS MASK AUTHTYPE MAP host rep 127.0.0.1 255.255.255 md5and then reload your database and test if it works.
dpavlin@luna:~$ sudo /etc/init.d/postgresql reload postmaster successfully signaled dpavlin@luna:~$ psql -U rserv -W -h 127.0.0.1 rep Password: enter BATyLAaf Welcome to psql, the PostgreSQL interactive terminal. ... rep=> \q
dpavlin@llin:~/rserv$ ssh -N -L 15432:localhost:5432 luna & [1] 5197 this number if PID of ssh. it will be different. dpavlin@llin:~/rserv$ tunnel_pid=$!
dpavlin@llin:~/rserv$ psql -U rserv -W -h 127.0.0.1 -p 15432 rep Password: enter BATyLAaf Welcome to psql 7.3.3, the PostgreSQL interactive terminal. ... rep=> \q
dpavlin@llin:~/rserv$ ./bin/SlaveInit --slavehost=localhost --slaveport=15432 --slaveuser=rserv --slavepassword=BATyLAaf rep rep
You can give a specific column name in the table, there is no restriction on this in this version (0.1), although the author's document says only this column will be replicated. Useful Bug !!! :-) Any column changed in the table will be properly updated in the master database.That is not true. This is not column name, but name of unique key. And since this data is used when using snapshots and updates I doubt that it will work correctly if you just enter anything. Also, it's quite possible that you will run into problems if you are using compound keys (that is, if your table has key which is composed of more than one field).
Since, I'm using really simple schema for this HOWTO, I do following:
dpavlin@llin:~/rserv$ ./bin/MasterAddTable rep a id NOTICE: CreateTrigger: changing return type of function _rserv_log_() from OPAQUE to TRIGGER dpavlin@llin:~/rserv$ ./bin/MasterAddTable rep b id dpavlin@llin:~/rserv$ ./bin/MasterAddTable rep a_b a_id dpavlin@llin:~/rserv$ ./bin/MasterAddTable rep c fooYou will notice that I'm not using unique key for table a_b. That is because you can specify just one column and unique key for that table is (a_id,b_id). You will also notice that table c has primary key id, and I'm entering column foo. That is intentional mistake so we can test what happens.
FIXME
./Replicate --slavehost=localhost --slaveuser=rserv --slavepassword=BATyLAaf --slaveport=15432 rep rep
$ kill $tunnel_pid