Guest User

Untitled

a guest
Feb 14th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #!/bin/bash
  2. set -Eeu -o pipefail
  3.  
  4. psqltmp() {
  5. (
  6. set -Eeu -o pipefail
  7.  
  8. local _pgcmd
  9. if [ -n "${1:-}" ]; then
  10. _pgcmd="${1}"
  11. elif which pgcli >/dev/null 2>&1; then
  12. _pgcmd=pgcli
  13. else
  14. _pgcmd=psql
  15. fi
  16.  
  17. local POSTGRES_USER POSTGRES_PASSWORD
  18. POSTGRES_USER=localuser
  19. POSTGRES_PASSWORD=password
  20. export POSTGRES_USER POSTGRES_PASSWORD
  21.  
  22. # Create a docker container for the DB.
  23. local _container_id
  24. _container_id="$(docker run -dP -e POSTGRES_USER -e POSTGRES_PASSWORD postgres:9.5)"
  25.  
  26. # When done, remove the container.
  27. trap 'docker rm -f "${_container_id}" >/dev/null' EXIT
  28.  
  29. # Get the bound localhost port corresponding to the standard 5432 postgres port of the container.
  30. local _pg_port _pg_uri
  31. _pg_port="$(docker inspect "${_container_id}" -f '{{index .NetworkSettings.Ports "5432/tcp" 0 "HostPort"}}')"
  32. _pg_uri="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:${_pg_port}"
  33.  
  34. # Wait for DB to start.
  35. while ! psql -q "${_pg_uri}" -c 'SELECT 1' >/dev/null 2>&1; do printf . >&2; sleep 0.1; done;
  36. printf '\n'
  37.  
  38. echo "${_pg_uri}"
  39. ${_pgcmd} "${_pg_uri}"
  40. )
  41. }
  42. psqltmp "$@"
Add Comment
Please, Sign In to add comment