-
Notifications
You must be signed in to change notification settings - Fork 15
/
configure-postgis.py
executable file
·63 lines (49 loc) · 1.51 KB
/
configure-postgis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
#
# Enable the PostGIS extension
#
import os
import psycopg2
from psycopg2.sql import Identifier
import sys
from urllib.parse import urlparse
def identifier(s, connection):
"""
Return s as a double-quoted string (good for psql identifiers)
"""
return Identifier(s).as_string(connection)
def postgis_sql(user, connection):
"""
Return some SQL to configure the PostGIS extension.
"""
template_filename = 'configure-postgis.sql'
with open(template_filename) as fp:
template = fp.read()
return template.format(user=identifier(user, connection))
def get_env(name):
if name not in os.environ:
raise Exception("Required environment variable %s is missing" % name)
value = os.environ[name]
if not value:
raise Exception("Environment variable %s missing value" % name)
return value
def main():
database_url = get_env('DATABASE_URL')
database = urlparse(database_url)
user = database.username
try:
with psycopg2.connect(database_url) as conn:
sql = postgis_sql(user, conn)
print("<SQL>")
print(sql)
print("</SQL>")
try:
with conn.cursor() as cur:
cur.execute(sql)
except Exception as sql_e:
print("Exception while executing SQL:", str(sql_e))
except Exception as conn_e:
print("Unable to connect to database:", str(conn_e))
sys.exit(-1)
if __name__ == '__main__':
main()