【问题标题】:connect pgbouncer and postgres running with docker连接使用 docker 运行的 pgbouncer 和 postgres
【发布时间】:2020-04-12 20:17:52
【问题描述】:

我目前尝试使用docker-compose 连接到,但pgbouncer 无法连接到postgresql

我尝试连接到pgbouncer 整个Postgresql bash:

$ sudo docker-compose ps 

        Name                      Command               State           Ports         
--------------------------------------------------------------------------------------
postgres_pgbouncer_1   /opt/pgbouncer/entrypoint.sh     Up      0.0.0.0:6432->6432/tcp
postgres_postgres_1    docker-entrypoint.sh postg ...   Up      0.0.0.0:5432->5432/tcp

$ sudo docker-compose exec postgres bash 
& psql -h pgbouncer -p 6432 -U postgre
psql: ERROR:  no such user: postgres

我在pgbouncer 日志中收到此警告:

$ sudo docker-compose logs -f pgbouncer

2019-12-20 08:11:31.802 UTC [1] WARNING C-0x5591f3329fb0: (nodb)/(nouser)@192.168.96.2:40270 pooler error: no such user: postgres

docker-compose.yml

version: '3.4'

networks:
  pushe-set: null

volumes:
  postgres-data: null


services:
  postgres:
    networks:
      - pushe-set
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./data/postgresql/scripts:/docker-entrypoint-initdb.d:ro
      - ./data/postgresql/config:/etc/postgresql:ro
    dns:
      - 8.8.8.8
      - 4.2.2.4
    restart: always
    image: postgres
    env_file:
      - env/postgresql.env
    ports:
      - 5432:5432
    command:
      - "postgres"
      - "-c"
      - "config_file=/etc/postgresql/postgresql.conf"

  pgbouncer:
    depends_on:
      - postgres
    networks:
      - pushe-set
    #volumes:
      #- ./data/pgbouncer/userlist.txt:/etc/pgbouncer/userlist.txt:ro
    dns:
      - 8.8.8.8
      - 4.2.2.4
    restart: always
    image: pgbouncer/pgbouncer
    env_file:
      - env/pgbouncer.env
    ports:
      - 6432:6432

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             0.0.0.0/0               md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     all                                     trust
#host    replication     all             127.0.0.1/32            trust
#host    replication     all             ::1/128                 trust

postgres 用户

root:/# psql -V
psql (PostgreSQL) 11.4 (Debian 11.4-1.pgdg90+1)

root:/# psql -U postgres
Password for user postgres: 
psql (11.4 (Debian 11.4-1.pgdg90+1))
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

postgres=# 

postgresql.evn

POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
PGDATA=/var/lib/postgresql/data

pgbouncer.ini

#pgbouncer.ini

[databases]
* = host = postgres port=5432 user=postgres password=postgres dbname=postgres

[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
auth_type = md5
max_client_conn = 400
default_pool_size = 20
min_pool_size = 10
reserve_pool_size = 10
max_db_connections = 50
max_user_connections = 50
ignore_startup_parameters = extra_float_digits

# Log settings
log_connections = 0
log_disconnections = 0
admin_users = postgres

pgbouncer.env

DATABASES_HOST=postgres
DATABASES_PORT=5432
DATABASES_USER=postgres
DATABASES_PASSWORD=postgres
DATABASES_DBNAME=postgres

PGBOUNCER_LISTEN_ADDE=*
PGBOUNCER_LISTEN_PORT=6432

PGBOUNCER_MAX_CLIENT_CONN=400
PGBOUNCER_DEFAULT_POOL_SIZE=20

PGBOUNCER_MIN_POOL_SIZE=10
PGBOUNCER_RESERVE_POOL_SIZE=10
PGBOUNCER_MAX_DB_CONNECTIONS=50
PGBOUNCER_MAX_USER_CONNECTIONS=50
PGBOUNCER_LOG_CONNECTIONS=0
PGBOUNCER_LOG_DISCONNECTIONS=0
PGBOUNCER_ADMIN_USERS=postgres
PGBOUNCER_AUTH_TYPE=md5

图片: postgresqlpgbouncer


更新

docker-compose up -d 
docker-compose logs -f postgres

postgres_1   | 
postgres_1   | Data page checksums are disabled.
postgres_1   | 
postgres_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1   | creating subdirectories ... ok
postgres_1   | selecting default max_connections ... 100
postgres_1   | selecting default shared_buffers ... 128MB
postgres_1   | selecting default timezone ... Etc/UTC
postgres_1   | selecting dynamic shared memory implementation ... posix
postgres_1   | creating configuration files ... ok
postgres_1   | running bootstrap script ... ok
postgres_1   | performing post-bootstrap initialization ... ok
postgres_1   | syncing data to disk ... ok
postgres_1   | 
postgres_1   | Success. You can now start the database server using:
postgres_1   | 
postgres_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1   | 
postgres_1   | 
postgres_1   | WARNING: enabling "trust" authentication for local connections
postgres_1   | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1   | --auth-local and --auth-host, the next time you run initdb.
postgres_1   | waiting for server to start....2019-12-20 10:47:39.557 UTC [42] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1   | 2019-12-20 10:47:39.606 UTC [43] LOG:  database system was shut down at 2019-12-20 10:47:39 UTC
postgres_1   | 2019-12-20 10:47:39.631 UTC [42] LOG:  database system is ready to accept connections
postgres_1   |  done
postgres_1   | server started
postgres_1   | 
postgres_1   | /usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init.sh
postgres_1   | /usr/local/bin/docker-entrypoint.sh: line 155: /docker-entrypoint-initdb.d/init.sh: Permission denied
postgres_1   | 2019-12-20 10:47:40.769 UTC [1] LOG:  listening on IPv4 address "127.0.0.1", port 5432
postgres_1   | 2019-12-20 10:47:40.769 UTC [1] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
postgres_1   | 2019-12-20 10:47:40.769 UTC [1] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
postgres_1   | 2019-12-20 10:47:40.776 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1   | 2019-12-20 10:47:40.848 UTC [24] LOG:  database system was interrupted; last known up at 2019-12-20 10:47:39 UTC
postgres_1   | 2019-12-20 10:47:40.885 UTC [24] LOG:  database system was not properly shut down; automatic recovery in progress
postgres_1   | 2019-12-20 10:47:40.889 UTC [24] LOG:  invalid record length at 0/1650E98: wanted 24, got 0
postgres_1   | 2019-12-20 10:47:40.889 UTC [24] LOG:  redo is not required
postgres_1   | 2019-12-20 10:47:40.933 UTC [1] LOG:  database system is ready to accept connections

【问题讨论】:

  • 如果你告诉我如何在我的系统上重新创建,我也许可以尝试
  • 感谢您的回复,我自定义了 PostgreSQL 配置,所以如果您忽略 docker-compose.ymlmkdir env 中的 volumescommands 并创建 postgresql.envpgbouncer,env 您可以重新创建运行此命令的情况docker-compose up -d
  • 知道如何知道 pgbouncer 已成功连接到 postgres!在答案中发布我的输出。
  • 你应该遍历 Postgres 终端(docker-compose exec postgres bash)并尝试向pgbouncer发送请求
  • psql -h pgbouncer -p 6432 -U postgre

标签: pgbouncer postgresql postgresql docker docker-compose pgbouncer


【解决方案1】:

我运行了你的 docker-compose,虽然我的 postgres 出于某种原因退出了,但 pgbouncer 似乎与 postgres 连接良好。

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 2022-12-11
    • 2019-08-15
    • 2016-12-27
    • 2021-08-15
    • 2020-07-31
    • 2021-03-02
    • 1970-01-01
    • 2020-01-03
    相关资源
    最近更新 更多