diff options
Diffstat (limited to 'backend/scripts/setup_db.sh')
-rwxr-xr-x | backend/scripts/setup_db.sh | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/backend/scripts/setup_db.sh b/backend/scripts/setup_db.sh new file mode 100755 index 0000000..7388015 --- /dev/null +++ b/backend/scripts/setup_db.sh @@ -0,0 +1,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'"
\ No newline at end of file |