-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
36 lines (26 loc) · 906 Bytes
/
init_db.py
File metadata and controls
36 lines (26 loc) · 906 Bytes
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
import os
import psycopg2
def env_or_default(var_name, default):
environment_variable = os.getenv(var_name)
if environment_variable:
return environment_variable
return default
conn = psycopg2.connect(
host="localhost",
database="recommendation",
user= env_or_default('RECOMMENDATION_DB_USERNAME', "recommendation"),
password=env_or_default('RECOMMENDATION_DB_PASSWORD','recommendation'))
# Open a cursor to perform database operations
cur = conn.cursor()
# Execute a command: this creates a new table
cur.execute('DROP TABLE IF EXISTS dancer;')
cur.execute('CREATE TABLE dancer (id uuid PRIMARY KEY,'
'name varchar (150) NOT NULL);'
)
# Insert data into the table
cur.execute('INSERT INTO dancer (id, name)'
'VALUES (\'bb30ae5a-4d1f-11ed-b142-67b8c323121c\' , \'marc\')'
)
conn.commit()
cur.close()
conn.close()