-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_postgres.py
More file actions
37 lines (30 loc) · 935 Bytes
/
Copy pathinit_postgres.py
File metadata and controls
37 lines (30 loc) · 935 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
37
import psycopg2
from config import DATABASE_URL
def init_postgresql():
conn = psycopg2.connect(DATABASE_URL)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS jobs(
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title TEXT,
description TEXT,
status VARCHAR(50),
user_id INT,
CONSTRAINT fk_jobs_user FOREIGN KEY (user_id) REFERENCES users (id)
)
""")
cursor.execute("SELECT COUNT(*) FROM users")
print(cursor.fetchone())
cursor.execute("SELECT COUNT(*) FROM jobs")
print(cursor.fetchone())
conn.commit()
conn.close()
print("PostgreSQL tables created successfully")
print(DATABASE_URL)