Files
punimtag/scripts/setup_postgresql.sh
T
ilia cb9e927ad9
CI / skip-ci-check (pull_request) Successful in 30s
CI / docker-ci (pull_request) Successful in 32s
CI / python-lint (pull_request) Successful in 32s
CI / secret-scan (pull_request) Successful in 39s
CI / viewer-unit (pull_request) Successful in 2m21s
CI / admin-unit (pull_request) Successful in 2m43s
CI / e2e (pull_request) Successful in 3m22s
Scrub docs/scripts of example passwords and host paths.
Stop shipping punimtag_password / admin defaults in install helpers and
docs; generate secrets at install time; require ADMIN_PASSWORD from env
(with test-only bootstrap in conftest).
2026-08-05 12:28:56 -04:00

80 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Setup script for PostgreSQL database for PunimTag
set -e
echo "🔧 Setting up PostgreSQL for PunimTag..."
gen_secret() {
if command -v openssl &> /dev/null; then
openssl rand -hex 16
else
head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n' | head -c 32
fi
}
# Check if PostgreSQL is installed
if ! command -v psql &> /dev/null; then
echo "📦 Installing PostgreSQL..."
sudo apt update
sudo apt install -y postgresql postgresql-contrib
echo "✅ PostgreSQL installed"
else
echo "✅ PostgreSQL is already installed"
fi
# Start PostgreSQL service
echo "🚀 Starting PostgreSQL service..."
sudo systemctl start postgresql
sudo systemctl enable postgresql
DB_PASSWORD="${PUNIMTAG_DB_PASSWORD:-$(gen_secret)}"
# Create database and user
echo "📝 Creating database and user..."
sudo -u postgres psql << EOF
-- Create user if it doesn't exist
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_user WHERE usename = 'punimtag') THEN
CREATE USER punimtag WITH PASSWORD '${DB_PASSWORD}';
END IF;
END
\$\$;
-- Create main database if it doesn't exist
SELECT 'CREATE DATABASE punimtag OWNER punimtag'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'punimtag')\gexec
-- Create auth database if it doesn't exist
SELECT 'CREATE DATABASE punimtag_auth OWNER punimtag'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'punimtag_auth')\gexec
-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE punimtag TO punimtag;
GRANT ALL PRIVILEGES ON DATABASE punimtag_auth TO punimtag;
\q
EOF
echo "✅ Database and user created"
echo ""
echo "📋 Database connection details:"
echo " Host: localhost"
echo " Port: 5432"
echo " Main Database: punimtag"
echo " Auth Database: punimtag_auth"
echo " User: punimtag"
echo " Password: (printed once below — put in .env, do not commit)"
echo " ${DB_PASSWORD}"
echo ""
echo "Example .env lines:"
echo " DATABASE_URL=postgresql+psycopg2://punimtag:<password>@localhost:5432/punimtag"
echo " DATABASE_URL_AUTH=postgresql+psycopg2://punimtag:<password>@localhost:5432/punimtag_auth"
echo ""
echo "✅ PostgreSQL setup complete!"
echo ""
echo "Next steps:"
echo "1. Put the password above into your .env (never commit .env)"
echo "2. Install python-dotenv if needed: pip install python-dotenv"
echo "3. Run your application — it will connect to PostgreSQL automatically"