2015-09-28 61 views
3

如何通过命令行或编程方式更改pg_hba.confpostgresql.conf中的设置(特别是从fabricfabricfabtools)?从命令行或以编程方式更改配置参数

我已经找到set_config,但似乎并没有为它需要重新启动服务器的参数工作。改变的参数是postgresql.conflisten_addressespg_hba.conf一个新的生产线,所以从我们的子网络连接将被接受。

这需要编写使用fabric部署脚本。因为数据库服务器可能与其他带有自己的配置参数的应用程序共享,所以不能复制模板文件,然后覆盖现有的*.conf文件。因此,现有的配置必须是已更改,而不是替换。

+1

http://www.postgresql.org/docs/current/static/sql-altersystem.html –

+0

这解决了'listen_addresses'部分,谢谢! – schreon

回答

3

这是当前工作的解决方案,结合了来自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,这将只是一个附加新的一行,而不是删除旧的。我相信一个更清洁的解决方案是可能的。

相关问题