已成功将postgreSQL 9.6.5连接到pgbouncer 1.7.2

已成功将postgreSQL 9.6.5连接到pgbouncer 1.7.2,postgresql,pgbouncer,Postgresql,Pgbouncer,问题: 我正在努力将pgbouncer1.7.2连接到一个可操作的postgresql 9.6.5数据库(Django/Python web app,Ubuntu14.04操作系统) 你能帮我解决这个问题吗 背景: PostgreSQL在没有pgbouncer的情况下运行良好。我正在使用设置pgbouncer。数据库以前是从另一台机器恢复的 所有东西都在一台机器上。我正在尝试unix套接字连接。尚未尝试TCP(但已打开) 数据库名为mydb。我的Django项目设置为通过用户ubuntu连接到

问题:

我正在努力将
pgbouncer1.7.2
连接到一个可操作的
postgresql 9.6.5
数据库(Django/Python web app,Ubuntu14.04操作系统)

你能帮我解决这个问题吗


背景:

PostgreSQL在没有pgbouncer的情况下运行良好。我正在使用设置pgbouncer。数据库以前是从另一台机器恢复的

所有东西都在一台机器上。我正在尝试unix套接字连接。尚未尝试TCP(但已打开)

数据库名为
mydb
。我的Django项目设置为通过用户
ubuntu
连接到它


我所尝试的:

当我尝试
psql-d mydb-p6432 ubuntu
(作为用户
ubuntu
)时,我得到:
psql:ERROR:pgbouncer无法连接到服务器

同时,
pgbouncer.log
显示:

01:39:56.428 78472 LOG C-0xfa51e0: mydb/ubuntu@unix(120837):6432 login attempt: db=mydb user=ubuntu tls=no
01:39:56.428 78472 LOG C-0xfa51e0: mydb/ubuntu@unix(120837):6432 closing because: pgbouncer cannot connect to server (age=0)
01:39:56.428 78472 WARNING C-0xfa51e0: mydb/ubuntu@unix(120837):6432 Pooler Error: pgbouncer cannot connect to server
01:40:11.428 78472 LOG S-0xfa0530: mydb/ubuntu@11.65.119.381:5432 closing because: connect failed (age=15)
请注意,
psql-d mydb-p5432 ubuntu
成功地将我登录到
mydb
(无需密码)。密码是否造成了这里的问题


接下来,如果我执行
pgbouncer-d pgbouncer.ini
(作为用户
ubuntu
),我会得到一个
权限被拒绝的错误:

2017-10-15 23:34:14.325 17606 FATAL Cannot open logfile: '/var/log/postgresql/pgbouncer.log': Permission denied
pgbouncer.log
中没有生成相应的日志行。文件
perms
设置如下:

ubuntu@ip-xxx-xx-xx-xx:/var/log/postgresql$ ls -lh
total 59M
-rw-r--r-- 1 postgres postgres 1.8M Oct 15 23:35 pgbouncer.log
-rw-r----- 1 postgres adm       57M Oct 15 23:07 postgresql-9.6-main.log

我配置的内容:

下面是我在Django应用程序的
settings.py
文件中的记录:

DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'mydb', 
                'USER': 'ubuntu',
                'PASSWORD': DB_PASSWORD,
                'HOST': '/var/run/postgresql',                 
                #'PORT': '6432',
        }
如果我取消注释“端口”:“6432”
,它仍然不起作用


Pgbouncer的
Pgbouncer.ini
包含以下内容:

[databases]
mydb= host=11.65.119.381 port=5432 user=ubuntu dbname=mydb

logfile = /var/log/postgresql/pgbouncer.log
pidfile = /var/run/postgresql/pgbouncer.pid

; ip address or * which means all ip-s
listen_addr = *
listen_port = 6432

; unix socket is also used for -R.
; On debian it should be /var/run/postgresql
;unix_socket_mode = 0777
;unix_socket_group =
unix_socket_dir = /var/run/postgresql

auth_type = trust
auth_file = /etc/pgbouncer/userlist.txt
admin_users = myuser, postgres, root
stats_users = myuser, postgres, root
auth_type
设置为
any
也不起作用


/etc/pgbouncer/userlist.txt
包含:

"ubuntu" "md565j6e98u1z098oiuyt7543poi4561yh3"
local   all             postgres                                peer

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 md5
listen_addresses = '*'
port = 5432 
unix_socket_directories = '/var/run/postgresql'
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
total 8.0K
drwxr-s--- 2 postgres postgres 120 Oct 16 00:06 9.6-main.pg_stat_tmp
-rw-r--r-- 1 postgres postgres   6 Oct 15 13:23 9.6-main.pid
-rw-r--r-- 1 postgres postgres   6 Oct 15 23:23 pgbouncer.pid
这里我通过
选择usename,passwd从pg_shadow那里获得密码字符串,usename='ubuntu'。请注意,这与Django的
settings.py
中引用的
DB_PASSWORD
不同(我也在
userlist.txt
中尝试过,但没有成功)


pg_hba.conf
包含:

"ubuntu" "md565j6e98u1z098oiuyt7543poi4561yh3"
local   all             postgres                                peer

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 md5
listen_addresses = '*'
port = 5432 
unix_socket_directories = '/var/run/postgresql'
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
total 8.0K
drwxr-s--- 2 postgres postgres 120 Oct 16 00:06 9.6-main.pg_stat_tmp
-rw-r--r-- 1 postgres postgres   6 Oct 15 13:23 9.6-main.pid
-rw-r--r-- 1 postgres postgres   6 Oct 15 23:23 pgbouncer.pid

postgresql.conf
包含:

"ubuntu" "md565j6e98u1z098oiuyt7543poi4561yh3"
local   all             postgres                                peer

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 md5
listen_addresses = '*'
port = 5432 
unix_socket_directories = '/var/run/postgresql'
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
total 8.0K
drwxr-s--- 2 postgres postgres 120 Oct 16 00:06 9.6-main.pg_stat_tmp
-rw-r--r-- 1 postgres postgres   6 Oct 15 13:23 9.6-main.pid
-rw-r--r-- 1 postgres postgres   6 Oct 15 23:23 pgbouncer.pid

文件烫发:

/var/run/postgresql/
包含:

"ubuntu" "md565j6e98u1z098oiuyt7543poi4561yh3"
local   all             postgres                                peer

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 md5
listen_addresses = '*'
port = 5432 
unix_socket_directories = '/var/run/postgresql'
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
total 8.0K
drwxr-s--- 2 postgres postgres 120 Oct 16 00:06 9.6-main.pg_stat_tmp
-rw-r--r-- 1 postgres postgres   6 Oct 15 13:23 9.6-main.pid
-rw-r--r-- 1 postgres postgres   6 Oct 15 23:23 pgbouncer.pid

如果您通过Unix套接字成功连接到Postgres,您可以告诉pgbouncer也这样做,方法是将主机指定为套接字文件所在的目录(通常是/tmp,如果您自己编译过),尽管发行版将其放在了不同的位置

因此,请在您的pgbouncer.ini中尝试以下内容:

[databases]
mydb= host=/tmp dbname=mydb
当pgbouncer以用户ubuntu(在您的情况下)的身份运行时,您可能不需要设置用户名,并且您使用的是默认端口号,因此您不需要显式设置它

在研究生阶段,你要做什么

show unix_socket_directories;

查看实际套接字目录的位置。

您提供了很多信息!请在PostgreSQL中将
log\u connections
设置为
on
,并在连接尝试失败后查看PostgreSQL server日志中的内容。打开
log\u connections
。它是一个繁忙的服务器,因此它立即开始溢出
2017-10-18 06:55:28.130 UTC[75440][unknown]@[unknown]日志:收到的连接:主机=[local]2017-10-18 06:55:28.130 UTC[75440]ubuntu@mydb日志:已授权连接:user=ubuntu database=mydb
。接下来我尝试了
psql-dmydb-p6432 ubuntu
。postgresql日志中没有出现任何新内容,这些字符串一直在重复。在终端上时,我收到错误消息
psql:error:pgbouncer无法连接到服务器
@LaurenzAlbe:因此,在打开
log\u连接
后,端口
5432
上失败的连接确实会显示在postgresql的日志中。但是,通过端口
6432
连接失败并没有出现在同一个postgresql日志中-我刚刚确认了这一点。这是否意味着可以用来诊断问题?这意味着pgbouncer从未到达PostgreSQL server,因此其配置一定有问题。@LaurenzAlbe:理论上讲,这可能是端口/防火墙问题吗?在我的场景中,
pgbouncer.pid
9.6-main.pid
都位于
/var/run/postgresql
(此外,
显示unix\u套接字目录也指向此目录)。因此,在我的例子中,您建议我应该在pgbouncer.ini中设置
host=/var/run/postgresql
,并通过
sudo服务pgbouncer reload
重新加载pgbouncer?是的。只要将服务配置为使用与psql测试相同的用户,就可以了。你可能需要重新启动,而不是重新加载。太好了。这一更改允许我通过
psql-d mddb-p6432 ubuntu登录。Pgbouncer还没有投入使用。您能否快速查看我在ques中发布的Django conf,尤其是我评论过的端口?这是我需要取消注释才能继续的东西吗?我在一个生产环境中,所以在执行此操作之前需要格外小心。您注释的端口是pgbouncer在其创建的unix套接字上侦听连接的端口号(文件名)。您可以使用
ls-a/var/run/postgresql
查看这些文件。如果没有它,它将尝试连接到postgres服务器。也就是说,我不知道如何配置Django——只要参数被正确地发送到psychopg2,它们看起来就是正确的。