Files
lasuite-drive/bin/psql
T
Julien Maupetit d314565644 🔨(project) improve psql script
Make psql script more versatile and avoid unnecessary environment
variables parsing.
2026-09-09 11:07:24 +02:00

37 lines
924 B
Bash
Executable File

#!/usr/bin/env bash
#
# Run psql commands in the postgresql container.
#
# This scripts requires that the postgresql container is already running.
#
# Usage: bin/psql [OPTIONS] [ARG]
#
# If you need to run a psql shell session, you should define the TTY=1 environment variable:
#
# TTY=1 bin/psql
#
# You can also enforce the target database name using:
#
# POSTGRES_DB=drive_e2e bin/psql
#
# shellcheck source=bin/_config.sh
source "$(dirname "${BASH_SOURCE[0]}")/_config.sh"
declare cmd="psql -U \${POSTGRES_USER} -d \${POSTGRES_DB} -v ON_ERROR_STOP=1 $*"
declare -a opts
declare -i TTY=${TTY:-0}
declare POSTGRES_DB="${POSTGRES_DB:-drive}"
# Should we allocate a TTY?
if [[ ${TTY} -eq 0 ]]; then
opts+=("-T")
fi
# Enforce target database
opts+=("-e" "POSTGRES_DB=${POSTGRES_DB}")
echo "${opts}"
# Run psql command using the postgres user, not host user
_docker_compose exec "${opts[@]}" postgresql sh -c "${cmd}"