Skip to content

Instantly share code, notes, and snippets.

@italodr
Last active February 6, 2026 13:47
Show Gist options
  • Select an option

  • Save italodr/2ed6b61a5c9876722cae91f2cdfa76f0 to your computer and use it in GitHub Desktop.

Select an option

Save italodr/2ed6b61a5c9876722cae91f2cdfa76f0 to your computer and use it in GitHub Desktop.
Start a docker container with PostgresSQL DB. It can start from a backup.sql file
#!/usr/bin/env bash
set -e # Exit on any error
DB_CONTAINER_NAME="postgres-db"
DB_PASSWORD="password123"
POSTGRES_DB="postgres_db"
POSTGRES_PORT=5432
MAX_WAIT_TIME=60 # Maximum wait time in seconds
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if Docker is installed and running
if ! [ -x "$(command -v docker)" ]; then
log_error "Docker is not installed. Please install docker and try again."
log_info "Docker install guide: https://docs.docker.com/engine/install/"
exit 1
fi
PERSIST_VOLUME=true
UPDATE_BACKUP=false
for arg in "$@"; do
case $arg in
--empty|-e)
PERSIST_VOLUME=false
shift
;;
--update|-u)
UPDATE_BACKUP=true
shift
;;
esac
done
# If update backup is requested, only update and exit
if [ "$UPDATE_BACKUP" = true ]; then
echo "Updating backup..."
if [ "$(docker ps -q -f name=$DB_CONTAINER_NAME)" ]; then
docker exec $DB_CONTAINER_NAME pg_dumpall -U postgres > "$(dirname "$0")/backup.sql"
echo "Backup updated successfully"
exit 0
else
echo "Database container is not running. Cannot update backup."
exit 1
fi
fi
if [ "$(docker ps -q -f name=$DB_CONTAINER_NAME)" ]; then
docker start $DB_CONTAINER_NAME
echo "Database container started"
exit 0
fi
# Check if container exists but is stopped
if [ "$(docker ps -aq -f name=$DB_CONTAINER_NAME)" ]; then
log_info "Removing existing stopped container..."
docker rm $DB_CONTAINER_NAME
fi
set -a
source .env
log_info "Starting PostgreSQL container..."
log_info "Container name: $DB_CONTAINER_NAME"
log_info "Port: $POSTGRES_PORT"
log_info "Database: $POSTGRES_DB"
log_info "Persist volume: $PERSIST_VOLUME"
# Start the container
if [ "$PERSIST_VOLUME" = true ] && [ -f "$(dirname "$0")/backup.sql" ]; then
log_info "Creating container with backup data..."
if ! docker run --rm -d --name $DB_CONTAINER_NAME \
-e POSTGRES_PASSWORD=$DB_PASSWORD \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-e POSTGRES_DB=$POSTGRES_DB \
-p $POSTGRES_PORT:5432 \
docker.io/postgres:latest; then
log_error "Failed to start PostgreSQL container"
exit 1
fi
log_info "Waiting for PostgreSQL to be ready (max ${MAX_WAIT_TIME}s)..."
wait_time=0
until docker exec $DB_CONTAINER_NAME pg_isready -U postgres > /dev/null 2>&1; do
if [ $wait_time -ge $MAX_WAIT_TIME ]; then
log_error "PostgreSQL failed to start within ${MAX_WAIT_TIME} seconds"
log_info "Container logs:"
docker logs $DB_CONTAINER_NAME
exit 1
fi
sleep 1
wait_time=$((wait_time + 1))
if [ $((wait_time % 5)) -eq 0 ]; then
log_info "Still waiting... (${wait_time}s)"
fi
done
log_success "PostgreSQL is ready!"
log_info "Restoring backup data..."
if cat "$(dirname "$0")/backup.sql" | docker exec -i $DB_CONTAINER_NAME psql -U postgres > /dev/null 2>&1; then
log_success "Database container was successfully created with persisted data"
else
log_warning "Backup restoration failed, but container is running with empty database"
fi
else
log_info "Creating container with empty database..."
if ! docker run --rm -d --name $DB_CONTAINER_NAME \
-e POSTGRES_PASSWORD=$DB_PASSWORD \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-e POSTGRES_DB=$POSTGRES_DB \
-p $POSTGRES_PORT:5432 \
docker.io/postgres:latest; then
log_error "Failed to start PostgreSQL container"
exit 1
fi
log_info "Waiting for PostgreSQL to be ready (max ${MAX_WAIT_TIME}s)..."
wait_time=0
until docker exec $DB_CONTAINER_NAME pg_isready -U postgres > /dev/null 2>&1; do
if [ $wait_time -ge $MAX_WAIT_TIME ]; then
log_error "PostgreSQL failed to start within ${MAX_WAIT_TIME} seconds"
log_info "Container logs:"
docker logs $DB_CONTAINER_NAME
exit 1
fi
sleep 1
wait_time=$((wait_time + 1))
if [ $((wait_time % 5)) -eq 0 ]; then
log_info "Still waiting... (${wait_time}s)"
fi
done
log_success "Database container was successfully created with empty data"
fi
log_info "Database connection details:"
log_info " Host: localhost"
log_info " Port: $POSTGRES_PORT"
log_info " Database: $POSTGRES_DB"
log_info " Username: postgres"
log_info " Password: $DB_PASSWORD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment