微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgres 无法创建复制槽

如何解决postgres 无法创建复制槽

我正在尝试使用 中设置一个简单的复制场景。如您所见,我为主数据库(发布者)使用了 postgres 服务,为副本(订阅者)使用了一个 postgres 服务。 wal_level = logical 是通过使用此 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,(3,5.21,(4,6.34,(5,90.1,2),(6,87.1,(7,21,3),(8,23,(9,1,4),(10,2,(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;

replica-init.sql:

create table if not exists products
(
    product_id int
        constraint products_pk
            primary key,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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?