Initial commit

This commit is contained in:
2025-10-22 19:59:09 +00:00
commit 67996ade86
34 changed files with 905 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
#!/bin/bash
# Prompt for container name
read -p "Enter PostgreSQL container name: " CONTAINER_NAME
# Check if container is running
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" = "" ]; then
echo "Container $CONTAINER_NAME is not running!"
exit 1
fi
echo "Using container: $CONTAINER_NAME"
# Prompt for PostgreSQL root credentials
read -p "Enter PostgreSQL root username: " PGROOT_USER
# Prompt for database name
read -p "Enter new PostgreSQL database name: " DB_NAME
# Check if database exists
DB_EXISTS=$(docker exec $CONTAINER_NAME psql -U $PGROOT_USER -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'")
if [ "$DB_EXISTS" = "1" ]; then
echo "Database $DB_NAME already exists!"
exit 1
fi
# Create the database using docker exec and psql
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -c "CREATE DATABASE $DB_NAME;"
echo "Database $DB_NAME has been created successfully."

View File

@@ -0,0 +1,46 @@
#!/bin/bash
# Prompt for container name
read -p "Enter PostgreSQL container name: " CONTAINER_NAME
# Check if container is running
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" = "" ]; then
echo "Container $CONTAINER_NAME is not running!"
exit 1
fi
echo "Using container: $CONTAINER_NAME"
# Prompt for PostgreSQL root credentials
read -p "Enter PostgreSQL root username: " PGROOT_USER
# Prompt for username and password
read -p "Enter new PostgreSQL username: " DB_USER
# Check if username is provided
if [[ -z "$DB_USER" ]]; then
echo "Error: Username cannot be empty!" >&2
exit 1
fi
# Check if user already exists in PostgreSQL
if docker exec $CONTAINER_NAME psql -U $PGROOT_USER -tAc "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname='$DB_USER'" | grep -q 1; then
echo "Error: User '$DB_USER' already exists in PostgreSQL!" >&2
exit 1
fi
echo "User '$DB_USER' does not exist. Proceeding..."
read -s -p "Enter password for $DB_USER: " DB_PASS
echo
read -s -p "Confirm password for $DB_USER: " DB_PASS_CONFIRM
echo
# Check if passwords match
if [ "$DB_PASS" != "$DB_PASS_CONFIRM" ]; then
echo "Passwords do not match!"
exit 1
fi
# Create the user using docker exec and psql
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';"
echo "User $DB_USER has been created successfully."

View File

@@ -0,0 +1,110 @@
#!/bin/bash
set -e
# Prompt for container name
read -p "Enter PostgreSQL container name: " CONTAINER_NAME
# Check if username is provided
if [[ -z "$CONTAINER_NAME" ]]; then
echo "Error: Container cannot be empty!" >&2
exit 1
fi
# Check if container is running
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" = "" ]; then
echo "Container $CONTAINER_NAME is not running!"
exit 1
fi
echo "Using container: $CONTAINER_NAME"
# Prompt for PostgreSQL root credentials
read -p "Enter PostgreSQL root username: " PGROOT_USER
# Prompt for username to drop
read -p "Enter PostgreSQL username to drop: " DB_USER
# Check if username is provided
if [[ -z "$DB_USER" ]]; then
echo "Error: Username cannot be empty!" >&2
exit 1
fi
# Check if user exists in PostgreSQL
USER_EXISTS=$(docker exec "$CONTAINER_NAME" psql -U "$PGROOT_USER" -tAc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER';")
if [[ "$USER_EXISTS" != "1" ]]; then
echo "Error: User '$DB_USER' does not exist in PostgreSQL!" >&2
exit 1
fi
# # Check if user exists in PostgreSQL
# if ! docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $CONTAINER_NAME -tAc "SELECT 1 FROM pg_catalog.pg_roles WHERE rolname='$DB_USER'" | grep -q 1; then
# echo "Error: User '$DB_USER' does not exist in PostgreSQL!" >&2
# exit 1
# fi
echo "User '$DB_USER' exists."
# Get all databases (not just those with CONNECT privilege)
echo "Checking databases..."
ALL_DATABASES=$(docker exec $CONTAINER_NAME psql -U "$PGROOT_USER" -tAc "
SELECT datname
FROM pg_database
WHERE datname NOT IN ('template0', 'template1')
AND datallowconn = true;
")
# Check for database privileges
echo "Checking privileges for user '$DB_USER'..."
DATABASES=$(docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $CONTAINER_NAME -tAc "
SELECT datname
FROM pg_database d
WHERE has_database_privilege('$DB_USER', d.oid, 'CONNECT')
AND datname NOT IN ('template0', 'template1');
")
if [[ -n "$DATABASES" && "$DATABASES" != "" ]]; then
echo "WARNING: User '$DB_USER' has privileges on the following database(s):"
echo "$DATABASES"
echo ""
read -p "Do you want to proceed with removing this user? (yes/no): " CONFIRM
if [[ "$CONFIRM" != "yes" ]]; then
echo "Operation cancelled."
exit 0
fi
# Revoke privileges from each database
echo "Revoking privileges from databases..."
while IFS= read -r DB_NAME; do
if [[ -n "$DB_NAME" ]]; then
echo " - Revoking privileges on database: $DB_NAME"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $CONTAINER_NAME -c "REVOKE ALL PRIVILEGES ON DATABASE \"$DB_NAME\" FROM \"$DB_USER\";"
# Revoke privileges on all tables, sequences, and functions in public schema
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d "$DB_NAME" -c "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM \"$DB_USER\";"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d "$DB_NAME" -c "REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public FROM \"$DB_USER\";"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d "$DB_NAME" -c "REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public FROM \"$DB_USER\";"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d "$DB_NAME" -c "REVOKE ALL PRIVILEGES ON SCHEMA public FROM \"$DB_USER\";"
fi
done <<< "$DATABASES"
echo "Privileges revoked successfully."
else
echo "User '$DB_USER' has no database privileges."
fi
# Reassign and drop owned objects in each database
echo "Reassigning and dropping owned objects in all databases..."
while IFS= read -r DB_NAME; do
if [[ -n "$DB_NAME" ]]; then
echo " - Processing database: $DB_NAME"
docker exec $CONTAINER_NAME psql -U "$PGROOT_USER" -d "$DB_NAME" -c "REASSIGN OWNED BY \"$DB_USER\" TO \"$PGROOT_USER\";" 2>/dev/null
docker exec $CONTAINER_NAME psql -U "$PGROOT_USER" -d "$DB_NAME" -c "DROP OWNED BY \"$DB_USER\";" 2>/dev/null
fi
done <<< "$ALL_DATABASES"
# Drop the user using docker exec and psql
echo "Dropping user '$DB_USER'..."
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $CONTAINER_NAME -c "DROP USER \"$DB_USER\";"
echo "User $DB_USER has been dropped successfully."

View File

@@ -0,0 +1,50 @@
#!/bin/bash
# Prompt for container name
read -p "Enter PostgreSQL container name: " CONTAINER_NAME
# Check if container is running
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" = "" ]; then
echo "Container $CONTAINER_NAME is not running!"
exit 1
fi
echo "Using container: $CONTAINER_NAME"
# Prompt for PostgreSQL root credentials
read -p "Enter PostgreSQL root username: " PGROOT_USER
# Prompt for username and database name
read -p "Enter PostgreSQL username to grant privileges: " USERNAME
read -p "Enter PostgreSQL database name will be granted on: " DB_NAME
# Check if inputs are not empty
if [ -z "$USERNAME" ] || [ -z "$DB_NAME" ]; then
echo "Error: Username and database name cannot be empty"
exit 1
fi
# Grant all privileges
echo "Granting all privileges on $DB_NAME to $USERNAME..."
# Grant database privileges
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -c "GRANT CONNECT ON DATABASE $DB_NAME TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -c "ALTER DATABASE $DB_NAME OWNER TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -c "ALTER USER $USERNAME CREATEDB;"
# Connect to the specific database and grant schema privileges
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "GRANT ALL PRIVILEGES ON SCHEMA public TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "GRANT CREATE ON SCHEMA public TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "GRANT CREATE ON DATABASE $DB_NAME TO $USERNAME;"
# Grant default privileges for future tables and sequences
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO $USERNAME;"
docker exec $CONTAINER_NAME psql -U $PGROOT_USER -d $DB_NAME -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON SEQUENCES TO $USERNAME;"
if [ $? -eq 0 ]; then
echo "Successfully granted all privileges"
else
echo "Error granting privileges"
exit 1
fi