-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
52 lines (41 loc) · 1.47 KB
/
db.py
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
import psycopg2
from psycopg2 import sql, extras
import sys
def setup_db():
try:
# Establish a connection to the PostgreSQL database
connection = psycopg2.connect(
user='tapasmohanty',
password='postgres',
host='localhost',
port='5432',
database='face'
)
cursor = connection.cursor()
# Create the cube extension if it doesn't already exist
cursor.execute("CREATE EXTENSION IF NOT EXISTS cube;")
# Drop the 'vectors' table if it exists and create a new one
cursor.execute("DROP TABLE IF EXISTS vectors;")
cursor.execute("""
CREATE TABLE vectors (
id SERIAL PRIMARY KEY,
file VARCHAR,
vec_low CUBE,
vec_high CUBE
);
""")
# Create an index for the 'vectors' table to speed up search operations
cursor.execute("CREATE INDEX vectors_vec_idx ON vectors (vec_low, vec_high);")
# Commit the transaction
connection.commit()
print("Database setup completed successfully.")
except (Exception, psycopg2.DatabaseError) as error:
print(f"Error setting up the database: {error}")
sys.exit(1)
finally:
# Close the cursor and connection to clean up
if cursor:
cursor.close()
if connection:
connection.close()
setup_db()