Skip to content

Postgres Convenience Script

Beau Barker edited this page Oct 20, 2025 · 3 revisions

For development, add the following script to make it easy to run psql inside the container:

db/bin/postgres

#!/bin/bash

# Default to psql if first argument is not a known Postgres binary
if [[ $# -eq 0 || "$1" == -* ]]; then
  set -- psql "$@"
fi

# If input is coming from a pipe or file, use -T to disable tty
if [[ ! -t 0 ]]; then
  docker compose exec -T postgres "$@"
else
  docker compose exec -it postgres "$@"
fi

Make it executable:

chmod +x db/bin/postgres

To connect interactively (from the db directory):

bin/postgres

Example output:

psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.

app=#

🗒️ By default, bin/postgres opens a psql shell. You can also run other commands in the container like bin/postgres bash if needed, or psql explicitly with bin/postgres psql.

🔹 Run Inline SQL Commands

You can also run SQL directly without opening an interactive shell:

bin/postgres -c 'select * from movie;'

✅ Because bin/postgres defaults to psql, you don’t need to type psql explicitly.

Clone this wiki locally