Merge pull request #876 from elventear/postgresql_fix
Changes to postgresql to support defaults settings and older psycopg2
This commit is contained in:
commit
a2a8cfe099
1 changed files with 18 additions and 5 deletions
|
@ -75,13 +75,26 @@ def main():
|
||||||
encoding = module.params["encoding"]
|
encoding = module.params["encoding"]
|
||||||
state = module.params["state"]
|
state = module.params["state"]
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
|
# To use defaults values, keyword arguments must be absent, so
|
||||||
|
# check which values are empty and don't include in the **kw
|
||||||
|
# dictionary
|
||||||
|
params_map = {
|
||||||
|
"login_host":"host",
|
||||||
|
"login_user":"user",
|
||||||
|
"login_password":"password"
|
||||||
|
}
|
||||||
|
kw = dict( (params_map[k], v) for (k, v) in module.params.iteritems()
|
||||||
|
if k in params_map and v != '' )
|
||||||
try:
|
try:
|
||||||
db_connection = psycopg2.connect(host=module.params["login_host"],
|
db_connection = psycopg2.connect(database="template1", **kw)
|
||||||
user=module.params["login_user"],
|
|
||||||
password=module.params["login_password"],
|
|
||||||
database="template1")
|
|
||||||
# Enable autocommit so we can create databases
|
# Enable autocommit so we can create databases
|
||||||
db_connection.autocommit = True
|
if psycopg2.__version__ >= '2.4.2':
|
||||||
|
db_connection.autocommit = True
|
||||||
|
else:
|
||||||
|
db_connection.set_isolation_level(psycopg2
|
||||||
|
.extensions
|
||||||
|
.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
cursor = db_connection.cursor()
|
cursor = db_connection.cursor()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg="unable to connect to database: %s" % e)
|
module.fail_json(msg="unable to connect to database: %s" % e)
|
||||||
|
|
Loading…
Reference in a new issue