Created
          September 7, 2025 16:40 
        
      - 
      
 - 
        
Save sunderee/5ad07cf9fceafc89e229436e0272ca88 to your computer and use it in GitHub Desktop.  
    Bash script for creating or running your PostgreSQL in a Docker container.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env bash | |
| # Check if Docker is installed | |
| if ! command -v docker &> /dev/null; then | |
| echo "Docker is not installed. Please install Docker and try again." | |
| exit 1 | |
| fi | |
| # Function to validate required arguments | |
| validate_args() { | |
| local required_args=("$@") | |
| for arg in "${required_args[@]}"; do | |
| if [ -z "${!arg}" ]; then | |
| echo "Error: --$arg is required" | |
| exit 1 | |
| fi | |
| done | |
| } | |
| # Function to create and start a new PostgreSQL container | |
| create_container() { | |
| local host=$1 | |
| local port=$2 | |
| local db_name=$3 | |
| local username=$4 | |
| local password=$5 | |
| local container_name=$6 | |
| # Check if container already exists | |
| if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then | |
| echo "Error: Container ${container_name} already exists" | |
| exit 1 | |
| fi | |
| # Create and start the container | |
| docker run -d \ | |
| --name "${container_name}" \ | |
| -p "${host}:${port}:5432" \ | |
| -e POSTGRES_DB="${db_name}" \ | |
| -e POSTGRES_USER="${username}" \ | |
| -e POSTGRES_PASSWORD="${password}" \ | |
| postgres:latest | |
| if [ $? -eq 0 ]; then | |
| echo "Container ${container_name} created and started successfully" | |
| else | |
| echo "Error: Failed to create container ${container_name}" | |
| exit 1 | |
| fi | |
| } | |
| # Function to start an existing container | |
| run_container() { | |
| local container_name=$1 | |
| # Check if container exists | |
| if ! docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then | |
| echo "Error: Container ${container_name} does not exist" | |
| exit 1 | |
| fi | |
| # Start the container | |
| docker start "${container_name}" | |
| if [ $? -eq 0 ]; then | |
| echo "Container ${container_name} started successfully" | |
| else | |
| echo "Error: Failed to start container ${container_name}" | |
| exit 1 | |
| fi | |
| } | |
| # Parse command-line arguments | |
| action=$1 | |
| shift | |
| case "$action" in | |
| create) | |
| while [ $# -gt 0 ]; do | |
| case "$1" in | |
| --host) host=$2; shift 2 ;; | |
| --port) port=$2; shift 2 ;; | |
| --database-name) db_name=$2; shift 2 ;; | |
| --username) username=$2; shift 2 ;; | |
| --password) password=$2; shift 2 ;; | |
| --container-name) container_name=$2; shift 2 ;; | |
| *) echo "Unknown argument: $1"; exit 1 ;; | |
| esac | |
| done | |
| validate_args host port db_name username password container_name | |
| create_container "$host" "$port" "$db_name" "$username" "$password" "$container_name" | |
| ;; | |
| run) | |
| while [ $# -gt 0 ]; do | |
| case "$1" in | |
| --container-name) container_name=$2; shift 2 ;; | |
| *) echo "Unknown argument: $1"; exit 1 ;; | |
| esac | |
| done | |
| validate_args container_name | |
| run_container "$container_name" | |
| ;; | |
| *) | |
| echo "Usage: $0 {create|run} [--host HOST] [--port PORT] [--database-name DATABASE_NAME] [--username USERNAME] [--password PASSWORD] [--container-name CONTAINER_NAME]" | |
| exit 1 | |
| ;; | |
| esac | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment