Skip to content

Instantly share code, notes, and snippets.

@michaelkosir
Created October 14, 2025 15:50
Show Gist options
  • Select an option

  • Save michaelkosir/b39d7f1152ceb4f9e7e2b36b89786918 to your computer and use it in GitHub Desktop.

Select an option

Save michaelkosir/b39d7f1152ceb4f9e7e2b36b89786918 to your computer and use it in GitHub Desktop.
Quickly deploy Postgres development servers locally using Docker
# Usage:
# pdev
# pdev 17.6
# pdev list
# pdev list 18
# pdev stop
pdev() {
pattern='^[0-9]+\.[0-9]+(\.[0-9]+)?$'
if [[ -z $1 || $1 == "latest" || $1 =~ $pattern ]]; then
export PGUSER="postgres"
export PGPASSWORD="root"
if [ -z "$(docker ps -qf name=pdev)" ]; then
version=${1:-latest}
image="postgres:$version"
if [[ ! "$(docker images -q $image)" ]]; then
echo "🏗️ Preparing Development Environment for $image"
docker pull $image | awk '{print "| " $0}'
fi
docker run \
--rm \
--detach \
--name=pdev \
--publish=5432:5432 \
--net=kind \
--env="POSTGRES_PASSWORD=$PGPASSWORD" \
$image > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "❌ Failed to start Postgres dev server."
return 1
fi
echo "✅ Postgres dev server started!"
echo " Address: \033[36mhttp://localhost:5432\033[0m"
echo " Username: \033[33m$PGUSER\033[0m"
echo " Password: \033[33m$PGPASSWORD\033[0m"
fi
export PGHOST=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' pdev)
elif [[ $1 == "stop" ]]; then
unset PGHOST
unset PGUSER
unset PGPASSWORD
# if running, stop Postgres
if [ "$(docker ps -qf name=pdev)" ]; then
docker stop pdev > /dev/null 2>&1
fi
elif [[ $1 == "list" ]]; then
if [[ -n $2 ]]; then
VERSIONS=($(curl -s https://www.postgresql.org/docs/release/ | grep -o '/docs/release/[^/]\+/">' | sed 's|/docs/release/\(.*\)/">|\1|' | grep $2))
else
VERSIONS=($(curl -s https://www.postgresql.org/docs/release/ | grep -o '/docs/release/[^/]\+/">' | sed 's|/docs/release/\(.*\)/">|\1|'))
fi
REVERSED_VERSIONS=()
for (( i=${#VERSIONS[@]}-1; i>=0; i-- )); do
REVERSED_VERSIONS+=("${VERSIONS[i]}")
done
(IFS=$'\n'; printf "%s" "${REVERSED_VERSIONS[*]}")
else
echo "Usage: pdev [version] | stop | list [filter]"
echo "Version format: <major>.<minor>[.<patch>]"
echo "Examples:"
echo " pdev"
echo " pdev 17.6"
echo " pdev list"
echo " pdev list 18.0"
echo " pdev stop"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment