Skip to content

Instantly share code, notes, and snippets.

@JoooostB
Last active June 7, 2022 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoooostB/a114698de47aec5215a4a6715fb9f56a to your computer and use it in GitHub Desktop.
Save JoooostB/a114698de47aec5215a4a6715fb9f56a to your computer and use it in GitHub Desktop.
Readiness- & Livenessprobes for PostgreSQL on Kubernetes using environment variables

I always end up reinventing the wheel when setting up Readiness- & LivenessProbes for Postgres deployments on Kubernetes, which is why I decided to share it here for others to enjoy too.

As you'd like to use environment variables, make sure to prefix the readiness and liveness commands with bash -c:

readinessProbe:
  exec:
    command: ["bash", "-c", "psql -w -U $POSTGRES_USER -d $POSTGRES_DB SELECT 1"]
  initialDelaySeconds: 15
  timeoutSeconds: 2
livenessProbe:
  exec:
    command: ["bash", "-c", "psql -w -U $POSTGRES_USER -d $POSTGRES_DB SELECT 1"]
  initialDelaySeconds: 30
  timeoutSeconds: 2

In the example above the user from environment variable $POSTGRES_USER connects to a database from variable $POSTGRES_DB without a password.

If your user does have a password, you can supply it via the PG_PASSWORD environment variable. Remember to omit the “-w” flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment