Update postgres_new_db_user

This commit is contained in:
vijay 2026-01-23 05:44:35 +00:00
parent d54a695020
commit 04b3acc428

View File

@ -2,50 +2,96 @@
# wget -O /tmp/postgres_new_db_user.sh https://git.technozone.com.au/vijay/Scripts/raw/branch/main/postgres_new_db_user && bash /tmp/postgres_new_db_user.sh && rm -f /tmp/postgres_new_db_user.sh
# Make sure the script is ran within postgres docker container or postgres server installed.
echo "=== PostgreSQL Database & User Setup ==="
read -p "Enter new database name: " DB_NAME
read -p "Enter new username: " DB_USER
read -s -p "Enter new password: " DB_PASS
echo ""
read -p "Enter PostgreSQL superuser (default: postgres): " PG_SUPER
PG_SUPER=${PG_SUPER:-postgres}
#!/bin/sh
# Optional: change host/port if needed
PG_HOST="localhost"
PG_PORT="5432"
echo "=== PostgreSQL Docker Database & User Setup (POSIX SH Mode) ==="
printf "Enter new database name: "
read DB_NAME
printf "Enter new username: "
read DB_USER
printf "Enter new password: "
read DB_PASS
echo ""
echo "Creating user and database..."
echo "Scanning Docker containers exposing PostgreSQL ports (5432)..."
# Create user
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -tc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER';" | grep -q 1 \
|| psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';"
# Capture containers with 5432 published
CONTAINER_LIST=$(docker ps --format "{{.Names}} {{.Ports}}" | grep "5432/tcp" | awk '{print $1}')
# Create database
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -tc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME';" | grep -q 1 \
|| psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
# Check if any match
if [ -z "$CONTAINER_LIST" ]; then
echo "❌ No containers exposing port 5432 found."
echo "Available containers:"
docker ps --format " - {{.Names}} | {{.Image}} | {{.Ports}}"
exit 1
fi
echo "Granting permissions..."
echo ""
echo "Select Postgres container:"
# Grant DB connect
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "GRANT CONNECT, TEMPORARY ON DATABASE $DB_NAME TO $DB_USER;"
i=1
echo "$CONTAINER_LIST" | while IFS= read -r cname; do
echo "$i) $cname"
i=$((i+1))
done
# Schema permissions
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "GRANT USAGE, CREATE ON SCHEMA public TO $DB_USER;"
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "ALTER SCHEMA public OWNER TO $DB_USER;"
# Re-print list into an indexed variable storage
i=1
for cname in $CONTAINER_LIST; do
eval "C$i=\"$cname\""
i=$((i+1))
done
# Existing tables & sequences
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;"
psql -U "$PG_SUPER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;"
TOTAL=$((i-1))
# Future tables & sequences
psql -U "$DB_USER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO $DB_USER;"
psql -U "$DB_USER" -h "$PG_HOST" -p "$PG_PORT" -d "$DB_NAME" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON SEQUENCES TO $DB_USER;"
printf "Enter choice [1-$TOTAL]: "
read choice
# Validate choice
case $choice in
''|*[!0-9]*)
echo "❌ Invalid input"
exit 1
;;
esac
if [ "$choice" -lt 1 ] || [ "$choice" -gt "$TOTAL" ]; then
echo "❌ Choice out of range"
exit 1
fi
# Get selected container
eval "PG_CONTAINER=\$C$choice"
echo ""
echo "✔ Selected container: $PG_CONTAINER"
# Wrapper for docker psql
run_psql() {
docker exec -i "$PG_CONTAINER" sh -c "$1"
}
echo "Creating role if missing..."
run_psql "psql -U postgres -tc \"SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'\" | grep -q 1 || psql -U postgres -c \"CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';\""
echo "Creating database if missing..."
run_psql "psql -U postgres -tc \"SELECT 1 FROM pg_database WHERE datname='$DB_NAME'\" | grep -q 1 || psql -U postgres -c \"CREATE DATABASE $DB_NAME OWNER $DB_USER;\""
echo "Granting privileges..."
run_psql "psql -U postgres -d $DB_NAME -c \"GRANT CONNECT, TEMPORARY ON DATABASE $DB_NAME TO $DB_USER;\""
run_psql "psql -U postgres -d $DB_NAME -c \"GRANT USAGE, CREATE ON SCHEMA public TO $DB_USER;\""
run_psql "psql -U postgres -d $DB_NAME -c \"ALTER SCHEMA public OWNER TO $DB_USER;\""
run_psql "psql -U postgres -d $DB_NAME -c \"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $DB_USER;\""
run_psql "psql -U postgres -d $DB_NAME -c \"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $DB_USER;\""
run_psql "PGPASSWORD='$DB_PASS' psql -U $DB_USER -d $DB_NAME -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO $DB_USER;\""
run_psql "PGPASSWORD='$DB_PASS' psql -U $DB_USER -d $DB_NAME -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON SEQUENCES TO $DB_USER;\""
echo ""
echo "=== Setup Complete ==="
echo "Database: $DB_NAME"
echo "User: $DB_USER"
echo "Schema owner: $DB_USER"
echo "Full R/W on existing + future tables granted."
echo "Container: $PG_CONTAINER"
echo "Permissions: Full access granted"