This repository was archived by the owner on Apr 20, 2026. It is now read-only.
forked from CodeDrome/postgresql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgddl.py
More file actions
executable file
·60 lines (40 loc) · 2.25 KB
/
pgddl.py
File metadata and controls
executable file
·60 lines (40 loc) · 2.25 KB
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
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import psycopg2
import pgconnection
def main():
"""
Demonstrate creation of PostgreSQL database, tables and views using psycopg2
"""
print("------------------")
print("| codedrome.com |")
print("| PostgreSQL DDL |")
print("------------------\n")
# Create a tuple of dictionaries containing the SQL to create database, tables and views
queries = ({"Description": "Create database",
"Database": "postgres",
"SQL": "CREATE DATABASE codeinpython"},
{"Description": "Create galleries table ",
"Database": "codeinpython",
"SQL": "CREATE TABLE galleries(galleryid serial PRIMARY KEY, name varchar(64) NOT NULL, description varchar(256))"},
{"Description": "Create photos table ",
"Database": "codeinpython",
"SQL": "CREATE TABLE photos(photoid serial PRIMARY KEY, galleryid smallint REFERENCES galleries(galleryid) NOT NULL, title varchar(64) NOT NULL, description varchar(256) NOT NULL, photographer varchar(64) NOT NULL, datetaken date)"},
{"Description": "Create typesdemo table ",
"Database": "codeinpython",
"SQL": "CREATE TABLE typesdemo(serialid serial PRIMARY KEY, intcolumn integer, realcolumn real, varcharcolumn varchar(64), datecolumn date, booleancolumn boolean)"},
{"Description": "Create view galleriesphotos ",
"Database": "codeinpython",
"SQL": "CREATE VIEW galleriesphotos AS SELECT galleries.name AS galleryname, galleries.description AS gallerydescription, photos.title AS phototitle, photos.description AS photodescription FROM galleries LEFT JOIN photos ON photos.galleryid = galleries.galleryid"})
# iterate and run queries
try:
for query in queries:
conn = pgconnection.get_connection(query["Database"])
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute(query["SQL"])
print("Executed {}".format(query["Description"]))
cursor.close()
conn.close()
except psycopg2.ProgrammingError as e:
print(e)
main()