python create table postgresql

Solutions on MaxInterview for python create table postgresql by the best coders in the world

showing results for - "python create table postgresql"
Stefano
02 May 2017
1.wp-block-code {
2	border: 0;
3	padding: 0;
4}
5
6.wp-block-code > div {
7	overflow: auto;
8}
9
10.shcb-language {
11	border: 0;
12	clip: rect(1px, 1px, 1px, 1px);
13	-webkit-clip-path: inset(50%);
14	clip-path: inset(50%);
15	height: 1px;
16	margin: -1px;
17	overflow: hidden;
18	padding: 0;
19	position: absolute;
20	width: 1px;
21	word-wrap: normal;
22	word-break: normal;
23}
24
25.hljs {
26	box-sizing: border-box;
27}
28
29.hljs.shcb-code-table {
30	display: table;
31	width: 100%;
32}
33
34.hljs.shcb-code-table > .shcb-loc {
35	color: inherit;
36	display: table-row;
37	width: 100%;
38}
39
40.hljs.shcb-code-table .shcb-loc > span {
41	display: table-cell;
42}
43
44.wp-block-code code.hljs:not(.shcb-wrap-lines) {
45	white-space: pre;
46}
47
48.wp-block-code code.hljs.shcb-wrap-lines {
49	white-space: pre-wrap;
50}
51
52.hljs.shcb-line-numbers {
53	border-spacing: 0;
54	counter-reset: line;
55}
56
57.hljs.shcb-line-numbers > .shcb-loc {
58	counter-increment: line;
59}
60
61.hljs.shcb-line-numbers .shcb-loc > span {
62	padding-left: 0.75em;
63}
64
65.hljs.shcb-line-numbers .shcb-loc::before {
66	border-right: 1px solid #ddd;
67	content: counter(line);
68	display: table-cell;
69	padding: 0 0.75em;
70	text-align: right;
71	-webkit-user-select: none;
72	-moz-user-select: none;
73	-ms-user-select: none;
74	user-select: none;
75	white-space: nowrap;
76	width: 1%;
77}
78            
79                
80            
81         #!/usr/bin/python
82
83import psycopg2
84from config import config
85
86
87def create_tables():
88    """ create tables in the PostgreSQL database"""
89    commands = (
90        """
91        CREATE TABLE vendors (
92            vendor_id SERIAL PRIMARY KEY,
93            vendor_name VARCHAR(255) NOT NULL
94        )
95        """,
96        """ CREATE TABLE parts (
97                part_id SERIAL PRIMARY KEY,
98                part_name VARCHAR(255) NOT NULL
99                )
100        """,
101        """
102        CREATE TABLE part_drawings (
103                part_id INTEGER PRIMARY KEY,
104                file_extension VARCHAR(5) NOT NULL,
105                drawing_data BYTEA NOT NULL,
106                FOREIGN KEY (part_id)
107                REFERENCES parts (part_id)
108                ON UPDATE CASCADE ON DELETE CASCADE
109        )
110        """,
111        """
112        CREATE TABLE vendor_parts (
113                vendor_id INTEGER NOT NULL,
114                part_id INTEGER NOT NULL,
115                PRIMARY KEY (vendor_id , part_id),
116                FOREIGN KEY (vendor_id)
117                    REFERENCES vendors (vendor_id)
118                    ON UPDATE CASCADE ON DELETE CASCADE,
119                FOREIGN KEY (part_id)
120                    REFERENCES parts (part_id)
121                    ON UPDATE CASCADE ON DELETE CASCADE
122        )
123        """)
124    conn = None
125    try:
126        # read the connection parameters
127        params = config()
128        # connect to the PostgreSQL server
129        conn = psycopg2.connect(**params)
130        cur = conn.cursor()
131        # create table one by one
132        for command in commands:
133            cur.execute(command)
134        # close communication with the PostgreSQL database server
135        cur.close()
136        # commit the changes
137        conn.commit()
138    except (Exception, psycopg2.DatabaseError) as error:
139        print(error)
140    finally:
141        if conn is not None:
142            conn.close()
143
144
145if __name__ == '__main__':
146    create_tables()Code language: Python (python)