postgres 无法创建复制槽

postgres could not create replication slot

我正在尝试在 using and . As you can see I'm using a postgres service for the master database (publisher) and one more for the replica (subscriber). wal_level = logical is configured in both services by using this method 中设置一个简单的复制方案。

docker-compose.yml:

version: '3.3'

services:
  master:
    image: postgres:11
    env_file:
      - .env
    volumes:
      - ./postgres.conf:/etc/postgresql/postgresql.conf
      - ./master-init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
    - 5432:5432

  replica:
    image: postgres:11
    env_file:
      - .env
    volumes:
      - ./postgres.conf:/etc/postgresql/postgresql.conf
      - ./replica-init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
    - 6543:5432
    depends_on:
      - master

master-init.sql:

create table if not exists products
(
    product_id int
        constraint products_pk
            primary key,
    price      float not null,
    brand_id   int   not null
);

create table if not exists brand_avg
(
    brand_id  int unique not null,
    avg_price float      not null
);

create publication main_pub for all tables;

insert into products
    (product_id, price, brand_id)
values (1, 2.45, 1),
       (2, 4.21, 1),
       (3, 5.21, 1),
       (4, 6.34, 1),
       (5, 90.1, 2),
       (6, 87.1, 2),
       (7, 21, 3),
       (8, 23, 3),
       (9, 1, 4),
       (10, 2, 1),
       (11, 32, 3)
on conflict do nothing;

insert into brand_avg
    (brand_id, avg_price)
select brand_id, avg(price)
from products
group by brand_id
order by brand_id
on conflict do nothing;

副本-init.sql:

create table if not exists products
(
    product_id int
        constraint products_pk
            primary key,
    price      float not null,
    brand_id   int   not null
);

create table if not exists brand_avg
(
    brand_id  int unique not null,
    avg_price float      not null
);

create subscription main_sub connection 'dbname=random host=master user=sample password=1234asdf' publication main_pub;

postgres.conf:

listen_addresses = '*'
wal_level = logical

当我尝试启动服务时,出现此错误:

master_1   | 2021-02-04 19:02:07.549 UTC [90] ERROR:  logical decoding requires wal_level >= logical
replica_1  | psql:/docker-entrypoint-initdb.d/init.sql:16: ERROR:  could not create replication slot "main_sub": ERROR:  logical decoding requires wal_level >= logical
replica_1  | 2021-02-04 19:02:07.549 UTC [81] ERROR:  could not create replication slot "main_sub": ERROR:  logical decoding requires wal_level >= logical
replica_1  | 2021-02-04 19:02:07.549 UTC [81] STATEMENT:  create subscription main_sub connection 'dbname=random host=master user=sample password=1234asdf' publication main_pub;
db-rbr_replica_1 exited with code 3

下面是完整的标准输出:

$ docker-compose up
Creating network "db-rbr_default" with the default driver
Creating db-rbr_master_1 ... done
Creating db-rbr_replica_1 ... done
Attaching to db-rbr_master_1, db-rbr_replica_1
master_1   | The files belonging to this database system will be owned by user "postgres".
master_1   | This user must also own the server process.
master_1   |
master_1   | The database cluster will be initialized with locale "en_US.utf8".
master_1   | The default database encoding has accordingly been set to "UTF8".
master_1   | The default text search configuration will be set to "english".
master_1   |
master_1   | Data page checksums are disabled.
master_1   |
master_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
master_1   | creating subdirectories ... ok
master_1   | selecting default max_connections ... 100
master_1   | selecting default shared_buffers ... 128MB
master_1   | selecting default timezone ... Etc/UTC
master_1   | selecting dynamic shared memory implementation ... posix
master_1   | creating configuration files ... ok
master_1   | running bootstrap script ... ok
replica_1  | The files belonging to this database system will be owned by user "postgres".
replica_1  | This user must also own the server process.
replica_1  |
replica_1  | The database cluster will be initialized with locale "en_US.utf8".
replica_1  | The default database encoding has accordingly been set to "UTF8".
replica_1  | The default text search configuration will be set to "english".
replica_1  |
replica_1  | Data page checksums are disabled.
replica_1  |
replica_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
replica_1  | creating subdirectories ... ok
replica_1  | selecting default max_connections ... 100
replica_1  | selecting default shared_buffers ... 128MB
replica_1  | selecting default timezone ... Etc/UTC
replica_1  | selecting dynamic shared memory implementation ... posix
replica_1  | creating configuration files ... ok
replica_1  | running bootstrap script ... ok
master_1   | performing post-bootstrap initialization ... ok
master_1   | syncing data to disk ... ok
master_1   |
master_1   | Success. You can now start the database server using:
master_1   |
master_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
master_1   |
master_1   |
master_1   | WARNING: enabling "trust" authentication for local connections
master_1   | You can change this by editing pg_hba.conf or using the option -A, or
master_1   | --auth-local and --auth-host, the next time you run initdb.
master_1   | waiting for server to start....2021-02-04 19:02:06.696 UTC [46] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
master_1   | 2021-02-04 19:02:06.707 UTC [47] LOG:  database system was shut down at 2021-02-04 19:02:06 UTC
master_1   | 2021-02-04 19:02:06.710 UTC [46] LOG:  database system is ready to accept connections
master_1   |  done
master_1   | server started
replica_1  | performing post-bootstrap initialization ... ok
replica_1  | syncing data to disk ... ok
replica_1  |
replica_1  | Success. You can now start the database server using:
replica_1  |
replica_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
replica_1  |
replica_1  |
replica_1  | WARNING: enabling "trust" authentication for local connections
replica_1  | You can change this by editing pg_hba.conf or using the option -A, or
replica_1  | --auth-local and --auth-host, the next time you run initdb.
replica_1  | waiting for server to start....2021-02-04 19:02:07.050 UTC [46] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
replica_1  | 2021-02-04 19:02:07.060 UTC [47] LOG:  database system was shut down at 2021-02-04 19:02:06 UTC
replica_1  | 2021-02-04 19:02:07.064 UTC [46] LOG:  database system is ready to accept connections
master_1   | CREATE DATABASE
master_1   |
master_1   |
master_1   | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
replica_1  |  done
replica_1  | server started
master_1   | CREATE TABLE
master_1   | CREATE TABLE
master_1   | CREATE PUBLICATION
master_1   | INSERT 0 11
master_1   | INSERT 0 4
master_1   |
master_1   |
master_1   | waiting for server to shut down...2021-02-04 19:02:07.165 UTC [46] LOG:  received fast shutdown request
master_1   | .2021-02-04 19:02:07.166 UTC [46] LOG:  aborting any active transactions
master_1   | 2021-02-04 19:02:07.167 UTC [46] LOG:  background worker "logical replication launcher" (PID 53) exited with exit code 1
master_1   | 2021-02-04 19:02:07.168 UTC [48] LOG:  shutting down
master_1   | 2021-02-04 19:02:07.180 UTC [46] LOG:  database system is shut down
master_1   |  done
master_1   | server stopped
master_1   |
master_1   | PostgreSQL init process complete; ready for start up.
master_1   |
master_1   | 2021-02-04 19:02:07.280 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
master_1   | 2021-02-04 19:02:07.280 UTC [1] LOG:  listening on IPv6 address "::", port 5432
master_1   | 2021-02-04 19:02:07.282 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
master_1   | 2021-02-04 19:02:07.301 UTC [83] LOG:  database system was shut down at 2021-02-04 19:02:07 UTC
master_1   | 2021-02-04 19:02:07.305 UTC [1] LOG:  database system is ready to accept connections
replica_1  | CREATE DATABASE
replica_1  |
replica_1  |
replica_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
replica_1  | CREATE TABLE
replica_1  | CREATE TABLE
master_1   | 2021-02-04 19:02:07.549 UTC [90] ERROR:  logical decoding requires wal_level >= logical
replica_1  | psql:/docker-entrypoint-initdb.d/init.sql:16: ERROR:  could not create replication slot "main_sub": ERROR:  logical decoding requires wal_level >= logical
replica_1  | 2021-02-04 19:02:07.549 UTC [81] ERROR:  could not create replication slot "main_sub": ERROR:  logical decoding requires wal_level >= logical
replica_1  | 2021-02-04 19:02:07.549 UTC [81] STATEMENT:  create subscription main_sub connection 'dbname=random host=master user=sample password=1234asdf' publication main_pub;
db-rbr_replica_1 exited with code 3

您没有将自定义配置传递给 PG。

撰写文件应如下所示:

version: ...
services:
  master:
    ...
    command: ['-c', 'config_file=/etc/postgresql/postgresql.conf']
  
  replica:
    ...
    command: ['-c', 'config_file=/etc/postgresql/postgresql.conf']

让PG捡起来。

PG on docker hub "Database Configuration" section.