Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

postgresql standby replication with Docker

$
0
0

I am trying to do standby replication using postgresql. In my opinion my configuration is fine because any of db - master and slave don't returns any errors or warnings but the problem is that replication is not working - i made some changes on master db and that changes haven't been reflected on my slave db. Its weird because in log files i didn't see any errors. Maybe the problem is that i am using Docker for this but that fact should not matter.

Master:

FROM ubuntu

RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

USER postgres

RUN rm -rf /var/lib/postgresql/9.3/main/*

# /etc/postgresql/9.3/pg_hba.conf
RUN echo "host all  all    0.0.0.0/0  md5"           >> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "host replication repuser 172.17.0.3/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf

RUN echo "listen_addresses    = '*'"         >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_level           = hot_standby" >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "max_wal_senders     = 3"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_keep_segments   = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "checkpoint_segments = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "log_statement       = all"         >> /etc/postgresql/9.3/main/postgresql.conf

RUN /usr/lib/postgresql/9.3/bin/initdb /var/lib/postgresql/9.3/main/
RUN /etc/init.d/postgresql start && psql --command "CREATE USER repuser WITH SUPERUSER PASSWORD 'repuser';"

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
CMD bash -c "/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf;"

Slave:

FROM ubuntu

RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 python-psycopg2

USER postgres

RUN rm -rf /var/lib/postgresql/9.3/main/*
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf

# /etc/postgresql/9.3/main/postgresql.conf
RUN echo "hot_standby         = on"          >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_level           = hot_standby" >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "max_wal_senders     = 3"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_keep_segments   = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "checkpoint_segments = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "log_statement       = all"         >> /etc/postgresql/9.3/main/postgresql.conf

RUN touch ~/.pgpass
RUN echo "172.17.0.2:5432:replication:repuser:repuser" >> ~/.pgpass
RUN chmod 600 ~/.pgpass

# /etc/postgresql/9.3/main/recovery.conf
RUN echo "standby_mode     = on"                                                         >> /etc/postgresql/9.3/main/recovery.conf
RUN echo "primary_conninfo = 'host=172.17.0.2 port=5432 user=repuser password=repuserr'" >> /etc/postgresql/9.3/main/recovery.conf

RUN mkdir -p /var/run/postgresql/9.3-main.pg_stat_tmp
RUN chown postgres.postgres /var/run/postgresql/9.3-main.pg_stat_tmp -R

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
CMD bash -c "pg_basebackup -h 172.17.0.2 -D /var/lib/postgresql/9.3/main -U repuser -v -P --xlog-method=stream; /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf;"

Output:

master_1 | 2016-01-16 11:18:35 UTC [6-1] LOG:  database system was interrupted; last known up at 2016-01-16 11:01:25 UTC
master_1 | 2016-01-16 11:18:35 UTC [6-2] LOG:  database system was not properly shut down; automatic recovery in progress
master_1 | 2016-01-16 11:18:35 UTC [6-3] LOG:  redo starts at 0/17822A8
master_1 | 2016-01-16 11:18:35 UTC [6-4] LOG:  record with zero length at 0/1782618
master_1 | 2016-01-16 11:18:35 UTC [6-5] LOG:  redo done at 0/17825B8
master_1 | 2016-01-16 11:18:35 UTC [6-6] LOG:  last completed transaction was at log time 2016-01-16 11:01:27.628062+00
master_1 | 2016-01-16 11:18:35 UTC [6-7] LOG:  MultiXact member wraparound protections are now enabled
master_1 | 2016-01-16 11:18:35 UTC [10-1] LOG:  autovacuum launcher started
master_1 | 2016-01-16 11:18:35 UTC [5-1] LOG:  database system is ready to accept connections
slave_1  | transaction log start point: 0/2000028 on timeline 1
slave_1  | pg_basebackup: starting background WAL receiver
19459/19459 kB (100%), 1/1 tablespace                                         label)
slave_1  | transaction log end point: 0/20000F0
slave_1  | pg_basebackup: waiting for background process to finish streaming ...
slave_1  | pg_basebackup: base backup completed
slave_1  | 2016-01-16 11:18:36 UTC [9-1] LOG:  database system was interrupted; last known up at 2016-01-16 11:18:36 UTC
slave_1  | 2016-01-16 11:18:36 UTC [9-2] LOG:  redo starts at 0/2000028
slave_1  | 2016-01-16 11:18:36 UTC [9-3] LOG:  consistent recovery state reached at 0/20000F0
slave_1  | 2016-01-16 11:18:36 UTC [9-4] LOG:  redo done at 0/20000F0
slave_1  | 2016-01-16 11:18:36 UTC [9-5] LOG:  MultiXact member wraparound protections are now enabled
slave_1  | 2016-01-16 11:18:36 UTC [13-1] LOG:  autovacuum launcher started
slave_1  | 2016-01-16 11:18:36 UTC [8-1] LOG:  database system is ready to accept connections

i am really curious what is wrong here

UPDATE

I solved it. Instead of writing recovery.conf by myself i used -R option in pg_basebackup. So my slave configuration looks like this:

FROM ubuntu

RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 python-psycopg2

USER postgres

RUN rm -rf /var/lib/postgresql/9.3/main/*
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf

# /etc/postgresql/9.3/main/postgresql.conf
RUN echo "hot_standby         = on"          >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_level           = hot_standby" >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "max_wal_senders     = 3"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "wal_keep_segments   = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "checkpoint_segments = 8"           >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "log_statement       = all"         >> /etc/postgresql/9.3/main/postgresql.conf

RUN touch ~/.pgpass
RUN echo "172.17.0.2:5432:replication:repuser:repuser" >> ~/.pgpass
RUN chmod 600 ~/.pgpass

RUN mkdir -p /var/run/postgresql/9.3-main.pg_stat_tmp
RUN chown postgres.postgres /var/run/postgresql/9.3-main.pg_stat_tmp -R

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
CMD bash -c "pg_basebackup -h 172.17.0.2 -D /var/lib/postgresql/9.3/main -U repuser -v -P --xlog-method=stream -R; /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf;"

Everything is working fine right now.


Viewing all articles
Browse latest Browse all 17268

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>