Skip to content

Instantly share code, notes, and snippets.

@alex-sherwin
Last active September 12, 2017 16:41
Show Gist options
  • Save alex-sherwin/33d295d071ece29e72c55c29785bd939 to your computer and use it in GitHub Desktop.
Save alex-sherwin/33d295d071ece29e72c55c29785bd939 to your computer and use it in GitHub Desktop.
Execute postgresql psql with a single statement while setting statement_timeout
#!/bin/bash
# This is useful because when executing psql via "docker exec" because it can be quite painful to try and pipe in a
# multi-command statement, where -c only supports a single statement but is simpler to script inside a "docker exec"
# This example fully captures all stdout/stderr as well for logging purposes
# Note the -d/--dbname option supports connection strings from libpq
# see https://www.postgresql.org/message-id/577A8BBE.7010908%40mysoftforge.com
# This allows us to do things like set parameters such as statement_timeout in the connection parameters
result=$( { docker exec -i FIXME_CONTAINER_NAME psql --no-align --username=FIXME_DB_USERNAME --dbname="dbname=FIXME_DBNAME options='-c statement_timeout=0'" --quiet --tuples-only -c "VACUUM FULL VERBOSE FIXME_TABLE_NAME;"; } 2>&1 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment