-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_database.sh
executable file
·76 lines (63 loc) · 2.4 KB
/
setup_database.sh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# Variables
DB_SUPERUSER="ericcook" # Replace with your actual superuser
DB_NAME="file_vector"
DB_USER="ericcook"
DB_PASSWORD="" # Change this to your desired password
TABLE_NAME="embeddings_table"
VECTOR_DIM=768 # Adjust based on your embedding dimensions
# Ensure PostgreSQL is installed
if ! command -v psql &> /dev/null; then
echo "PostgreSQL is not installed. Please install it before proceeding."
exit 1
fi
# Install pgvector if not already installed
echo "Ensuring pgvector extension is enabled..."
psql -U $DB_SUPERUSER -c "CREATE EXTENSION IF NOT EXISTS vector;" || {
echo "Failed to enable pgvector. Ensure it's installed correctly and PostgreSQL is restarted."
exit 1
}
# Create PostgreSQL database if it doesn't exist
echo "Checking if database $DB_NAME exists..."
DB_EXISTS=$(psql -U $DB_SUPERUSER -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME';")
if [ "$DB_EXISTS" == "1" ]; then
echo "Database $DB_NAME already exists. Skipping creation."
else
echo "Creating database $DB_NAME..."
psql -U $DB_SUPERUSER -c "CREATE DATABASE $DB_NAME;" || {
echo "Failed to create database $DB_NAME."
exit 1
}
fi
# Create the user with a password if it doesn't exist
echo "Checking if user $DB_USER exists..."
USER_EXISTS=$(psql -U $DB_SUPERUSER -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER';")
if [ "$USER_EXISTS" == "1" ]; then
echo "User $DB_USER already exists. Skipping creation."
else
echo "Creating user $DB_USER..."
psql -U $DB_SUPERUSER -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';" || {
echo "Failed to create user $DB_USER."
exit 1
}
fi
# Grant privileges to the user on the database
echo "Granting privileges to user $DB_USER on database $DB_NAME..."
psql -U $DB_SUPERUSER -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" || {
echo "Failed to grant privileges to user $DB_USER."
exit 1
}
# Connect to the database and create the embeddings table
echo "Ensuring table $TABLE_NAME exists in database $DB_NAME..."
psql -U $DB_SUPERUSER -d $DB_NAME -c "
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS $TABLE_NAME (
id SERIAL PRIMARY KEY,
file_path TEXT NOT NULL,
embedding VECTOR($VECTOR_DIM) NOT NULL -- Adjust the dimension based on your embeddings
);
" || {
echo "Failed to create table $TABLE_NAME."
exit 1
}
echo "Database and table setup complete."