aboutsummaryrefslogtreecommitdiffstats
path: root/backend/scripts/setup_db.sh
blob: 738801568522ba1dde5a4e858beb1f2c1f30ff92 (plain) (blame)
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
#!/bin/bash

# Script to set up the PostgreSQL database for the finance application

# Prompt for PostgreSQL user to use
read -p "Enter PostgreSQL admin username [postgres]: " PG_USER
PG_USER=${PG_USER:-postgres}

# Prompt for PostgreSQL password
read -s -p "Enter PostgreSQL password for $PG_USER: " PG_PASSWORD
echo

# Check if database exists
echo "Checking if database exists..."
if PGPASSWORD="$PG_PASSWORD" psql -U "$PG_USER" -h localhost -lqt | cut -d \| -f 1 | grep -qw finance; then
    echo "Database 'finance' already exists"
else
    echo "Creating database 'finance'..."
    PGPASSWORD="$PG_PASSWORD" createdb -U "$PG_USER" -h localhost finance
    if [ $? -ne 0 ]; then
        echo "Failed to create database. Please check your credentials."
        exit 1
    fi
    echo "Database 'finance' created successfully"
fi

# Create .env file with database connection details
echo "Creating .env file with database connection info..."
cat > .env <<EOF
# Database Connection
DATABASE_DSN=host=localhost user=$PG_USER password=$PG_PASSWORD dbname=finance port=5432 sslmode=disable TimeZone=UTC

# JWT Secret (for user authentication)
JWT_SECRET=$(openssl rand -hex 32)
EOF

echo ".env file created with your database connection details."
echo "You can now run the application with 'go run cmd/api/main.go'"