Initial postgres skill implementation
This commit is contained in:
128
scripts/autorun.sh
Normal file
128
scripts/autorun.sh
Normal file
@@ -0,0 +1,128 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
POSTGRES_VERSION="${POSTGRES_VERSION:-16}"
|
||||
POSTGRES_DATA_DIR="${POSTGRES_DATA_DIR:-/data/postgres}"
|
||||
POSTGRES_USER="${POSTGRES_USER:-vibestack}"
|
||||
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-vibestack}"
|
||||
POSTGRES_DB="${POSTGRES_DB:-vibestack}"
|
||||
POSTGRES_INITDB_ARGS="${POSTGRES_INITDB_ARGS:-}"
|
||||
|
||||
# Idempotent PostgreSQL installation
|
||||
install_postgres() {
|
||||
if command -v psql &>/dev/null; then
|
||||
echo "PostgreSQL already installed: $(psql --version)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Installing PostgreSQL ${POSTGRES_VERSION}..."
|
||||
|
||||
# Install dependencies and add PostgreSQL repo
|
||||
apt-get update
|
||||
apt-get install -y curl ca-certificates gnupg lsb-release
|
||||
|
||||
# Add PostgreSQL APT repository
|
||||
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg
|
||||
echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
|
||||
|
||||
# Install PostgreSQL
|
||||
apt-get update
|
||||
apt-get install -y "postgresql-${POSTGRES_VERSION}"
|
||||
|
||||
echo "PostgreSQL installed: $(psql --version)"
|
||||
}
|
||||
|
||||
# Setup directories and permissions
|
||||
setup_dirs() {
|
||||
mkdir -p "$POSTGRES_DATA_DIR"
|
||||
chown -R postgres:postgres "$POSTGRES_DATA_DIR"
|
||||
chmod 700 "$POSTGRES_DATA_DIR"
|
||||
|
||||
echo "PostgreSQL data directory: $POSTGRES_DATA_DIR"
|
||||
}
|
||||
|
||||
# Initialize database if not exists
|
||||
init_database() {
|
||||
if [ -f "$POSTGRES_DATA_DIR/PG_VERSION" ]; then
|
||||
echo "Database already initialized"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Initializing database..."
|
||||
|
||||
# Initialize with specified args
|
||||
su - postgres -c "/usr/lib/postgresql/${POSTGRES_VERSION}/bin/initdb -D '$POSTGRES_DATA_DIR' $POSTGRES_INITDB_ARGS"
|
||||
|
||||
# Configure PostgreSQL
|
||||
cat >> "$POSTGRES_DATA_DIR/postgresql.conf" << EOF
|
||||
|
||||
# VibeStack configuration
|
||||
listen_addresses = '*'
|
||||
port = ${POSTGRES_PORT:-5432}
|
||||
max_connections = ${POSTGRES_MAX_CONNECTIONS:-100}
|
||||
EOF
|
||||
|
||||
# Configure authentication
|
||||
cat > "$POSTGRES_DATA_DIR/pg_hba.conf" << EOF
|
||||
# TYPE DATABASE USER ADDRESS METHOD
|
||||
local all postgres peer
|
||||
local all all md5
|
||||
host all all 127.0.0.1/32 md5
|
||||
host all all ::1/128 md5
|
||||
host all all 0.0.0.0/0 md5
|
||||
EOF
|
||||
|
||||
echo "Database initialized"
|
||||
}
|
||||
|
||||
# Create default user and database
|
||||
create_user_and_db() {
|
||||
local pg_ctl="/usr/lib/postgresql/${POSTGRES_VERSION}/bin/pg_ctl"
|
||||
local psql_cmd="/usr/lib/postgresql/${POSTGRES_VERSION}/bin/psql"
|
||||
|
||||
# Start PostgreSQL temporarily
|
||||
echo "Starting PostgreSQL for initial setup..."
|
||||
su - postgres -c "$pg_ctl -D '$POSTGRES_DATA_DIR' -w start"
|
||||
|
||||
# Create user if not exists
|
||||
if ! su - postgres -c "$psql_cmd -tAc \"SELECT 1 FROM pg_roles WHERE rolname='$POSTGRES_USER'\"" | grep -q 1; then
|
||||
echo "Creating user: $POSTGRES_USER"
|
||||
su - postgres -c "$psql_cmd -c \"CREATE USER $POSTGRES_USER WITH PASSWORD '$POSTGRES_PASSWORD'\""
|
||||
fi
|
||||
|
||||
# Create database if not exists
|
||||
if ! su - postgres -c "$psql_cmd -tAc \"SELECT 1 FROM pg_database WHERE datname='$POSTGRES_DB'\"" | grep -q 1; then
|
||||
echo "Creating database: $POSTGRES_DB"
|
||||
su - postgres -c "$psql_cmd -c \"CREATE DATABASE $POSTGRES_DB OWNER $POSTGRES_USER\""
|
||||
fi
|
||||
|
||||
# Grant privileges
|
||||
su - postgres -c "$psql_cmd -c \"GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER\""
|
||||
|
||||
# Run init scripts if present
|
||||
local skill_dir="$(dirname "$(dirname "$0")")"
|
||||
local initdb_dir="$skill_dir/initdb.d"
|
||||
|
||||
if [ -d "$initdb_dir" ]; then
|
||||
echo "Running init scripts..."
|
||||
for f in "$initdb_dir"/*.sql; do
|
||||
if [ -f "$f" ]; then
|
||||
echo " Running: $(basename "$f")"
|
||||
su - postgres -c "PGPASSWORD='$POSTGRES_PASSWORD' $psql_cmd -U $POSTGRES_USER -d $POSTGRES_DB -f '$f'"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Stop PostgreSQL (run.sh will start it properly)
|
||||
echo "Stopping PostgreSQL after setup..."
|
||||
su - postgres -c "$pg_ctl -D '$POSTGRES_DATA_DIR' -w stop"
|
||||
|
||||
echo "User and database setup complete"
|
||||
}
|
||||
|
||||
install_postgres
|
||||
setup_dirs
|
||||
init_database
|
||||
create_user_and_db
|
||||
|
||||
echo "PostgreSQL setup complete"
|
||||
Reference in New Issue
Block a user