29 lines
905 B
Bash
Executable File
29 lines
905 B
Bash
Executable File
#!/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."
|