从命令行或编程中更改配置参数

3
我如何从命令行或编程方式(尤其是通过fabric或fabtools)更改pg_hba.confpostgresql.conf中的设置?
我已经找到了set_config,但这似乎不适用于需要服务器重启的参数。要更改的参数是postgresql.conf中的listen_addressespg_hba.conf中的新行,因此将接受我们子网络的连接。
这是使用fabric编写部署脚本所需的。不能复制模板文件以覆盖现有的*.conf文件,因为数据库服务器可能与其他应用程序共享它们自己的配置参数。因此,现有配置必须被更改,而不是被替换。

1
http://www.postgresql.org/docs/current/static/sql-altersystem.html - user330315
那解决了 listen_addresses 的部分,谢谢! - schreon
1个回答

5

以下是目前运作的解决方案,包括来自a_horse_with_no_name的提示。这里贴出我们fabfile.py中的代码片段(它使用来自fabtoolsrequire,并针对Ubuntu运行):

db_name = env.variables['DB_NAME']
db_user = env.variables['DB_USER']
db_pass = env.variables['DB_PASSWORD']

# Require a PostgreSQL server.
require.postgres.server(version="9.4")
require.postgres.user(db_user, db_pass)
require.postgres.database(db_name, db_user)

# Listen on all addresses - use firewall to block inadequate access.
sudo(''' psql -c "ALTER SYSTEM SET listen_addresses='*';" ''', user='postgres')

# Download the remote pg_hba.conf to a temp file
tmp = tempfile.NamedTemporaryFile()
with open(tmp.name, "w") as f:
    get("/etc/postgresql/9.4/main/pg_hba.conf", f, use_sudo=True)

# Define the necessary line in pg_hba.conf.
hba_line = "host    all     all     {DB_ACCEPT_IP}/0   md5".format(**env.variables)

# Search the hba_line in the existing pg_hba.conf
with open(tmp.name, "ra") as f:
    for line in f:
        if hba_line in line:
           found = True
           break
    else:
        found = False

# If it does not exist, append it and upload the modified pg_hba.conf to the remote machine.
if not found:
    with open(tmp.name, "a") as f:
        f.write(hba_line)
    put(f.name, "/etc/postgresql/9.4/main/pg_hba.conf", use_sudo=True)

# Restart the postgresql service, so the changes take effect.
sudo("service postgresql restart")

我不喜欢这种方法的一个方面是,如果我更改 DB_ACCEPT_IP ,它只会添加一行新内容,而不是删除旧的。我相信有一种更清晰的解决方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接