使用 pgbouncer 时的 Django 设置

Django settings when using pgbouncer

我有一个带有 Postgresql 后端的 Django 网站,为此我使用 pgbouncer 数据库连接池(事务模式)。

应用程序和数据库驻留在不同的服务器上(各 1 个服务器)。我已经在应用程序服务器上安装了 pgbouncer。我的问题是:settings.py 中的配置应该是什么?请注意,我使用 Unix 套接字 连接到 pgbouncer。


我当前的 settings.py 包含:

DATABASE_URL = 'postgres://user1:pass1@xx.xxx.xxx.xxx:5432/db1'
DATABASES = {
'default': dj_database_url.config(default=DATABASE_URL)
}

pgbouncer.ini 的相关部分是:

[databases]
db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1

listen_addr = *
listen_port = 6432
auth_type = md5
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 200
default_pool_size = 300

userlist.txt 包含:

"user1" "pass1"

注意: 一个答案 is here,但对我不起作用,因为数据库在我的情况下在本地不可用。我需要设置 DATABASE_URL 环境变量,而不是使用 default = '...'.

一个建议似乎是将 pgbouncer 视为 settings.py 中的数据库。在那种情况下,像下面这样的东西会起作用吗?

if PRODUCTION == '1':
    #PRODUCTION is set to '1' if in production environment
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'pgbouncer',
            'USER': 'user1',
            'PASSWORD': 'pass1',
            'HOST': '/var/run/postgresql',
            'PORT': '6432',
        }

settings.py 中的所有数据库设置都应与 pgbouncer 配置中的设置相同,除了 settings.py 中的主机将指向 pgbouncer。您可能需要将 'NAME': 'pgbouncer' 更改为 'NAME': 'db1'。由于您使用的是 unix 套接字,因此端口无关紧要。

来自docs

pgbouncer is a PostgreSQL connection pooler. Any target application can be connected to pgbouncer as if it were a PostgreSQL server, and pgbouncer will create a connection to the actual server, or it will reuse one of its existing connections.

此外,

Have your application (or the psql client) connect to pgbouncer instead of directly to PostgreSQL server.


配置:

pgbouncer.ini:一个示例 pgbouncer.ini,其中包含有关默认值的注释

[databases]
db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1

[pgbouncer]
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = userlist.txt
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 100
default_pool_size = 20

userlist.txt:

"user1" "pass1"

输入settings.py:

if PRODUCTION == '1':
    #PRODUCTION is set to '1' if in production environment
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'db1',
            'USER': 'user1',
            'PASSWORD': 'pass1',
            'HOST': '/var/run/postgresql',
            # 'PORT': '6432',
        }

额外:

如果不使用 unix 套接字 - 你可以设置 HOST : '127.0.0.1' 或 'localhost' 如果 pgbouncer 在本地是 运行,或者服务器 pgbouncer 的 IP 是 运行 上。 来自 docs:

If you’re using PostgreSQL, by default (empty HOST), the connection to the database is done through UNIX domain sockets (‘local’ lines in pg_hba.conf). If your UNIX domain socket is not in the standard location, use the same value of unix_socket_directory from postgresql.conf. If you want to connect through TCP sockets, set HOST to ‘localhost’ or ‘127.0.0.1’ (‘host’ lines in pg_hba.conf). On Windows, you should always define HOST, as UNIX domain sockets are not available.


如果是 postgreSQL,对于 ENGINE,您可以使用 postgresqlpostgresql_psycopg2 - 鉴于您的 Django 版本,两者之间存在差异 - postgresql_psycopg2 vs posgresql.