如何通过命令行或编程方式(尤其是通过或)更改pg_hba.conf设置?postgresql.conffabricfabtools
我已经找到了set_config,但这似乎不适用于需要重新启动服务器的参数。要更改的参数位于listen_addresses中postgresql.conf,新行位于 中pg_hba.conf,因此来自我们子网的连接将被接受。
这是使用 编写部署脚本所必需的fabric。复制模板文件然后覆盖现有文件不是一个选项*.conf,因为数据库服务器可能与带有自己的配置参数的其他应用程序共享。因此,现有的配置必须改变,而不是替换。
这是当前有效的解决方案,结合了 a_horse_with_no_name 的提示。我粘贴了我们的代码片段fabfile.py(它使用requirefrom fabtools,并且它针对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")
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个解决方案的一个方面是,如果我更改DB_ACCEPT_IP,这只会附加一个新行,而不会删除旧行。我确信更清洁的解决方案是可能的。
| 归档时间: |
|
| 查看次数: |
2011 次 |
| 最近记录: |