-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
53 lines (46 loc) · 1.41 KB
/
app.py
File metadata and controls
53 lines (46 loc) · 1.41 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
import psycopg2
# Define connection parameters (this only needs to be done once)
host = "localhost"
port = 5432
dbname = "postgres"
user = "postgres"
password = "mysecretpassword"
def get_connection():
"""Establish a connection to the PostgreSQL database."""
return psycopg2.connect(
host=host,
port=port,
dbname=dbname,
user=user,
password=password
)
def create_table():
"""Create a 'Customers' table in the PostgreSQL database."""
try:
# Establish connection to the database
connection = get_connection()
cursor = connection.cursor()
# SQL query to create the table
create_table_query = """
CREATE TABLE IF NOT EXISTS Customers (
CustomerID SERIAL PRIMARY KEY,
FirstName VARCHAR(100),
LastName VARCHAR(100),
Email VARCHAR(100),
DateOfBirth DATE
);
"""
# Execute the query to create the table
cursor.execute(create_table_query)
connection.commit() # Commit the changes
print("Customers table created successfully")
except Exception as error:
print("Error while creating table:", error)
finally:
# Close the cursor and connection
if connection:
cursor.close()
connection.close()
# Run the function to create the table
if __name__ == "__main__":
create_table()