Skip to main content

alwaysApply: true

description: "PostgreSQL command execution guidelines for non-interactive environments"

PostgreSQL command execution rules

When running PostgreSQL commands, always follow these guidelines:

psql commands

  • Always use the --no-psqlrc flag when executing psql commands
  • Set PAGER=cat environment variable to avoid interactive pagers
  • For kubectl exec scenarios, use timeout command to prevent hanging in interactive mode
  • Avoid -it flags in kubectl exec when running non-interactive queries
  • For commands that might produce long output, pipe to | cat to ensure non-interactive behavior

Examples

# Correct way to run psql locally
PAGER=cat psql --no-psqlrc -d database_name -c "SELECT * FROM table;"

# For kubectl exec with psql - use timeout to prevent hanging in interactive mode
timeout 10 kubectl exec pod-name -n namespace -- env PAGER=cat psql --no-psqlrc -U user database -c "query"

# When using aliases like pgais
PAGER=cat pgais -c "SELECT version();"

Rationale

  • --no-psqlrc prevents loading user-specific psql configuration that might interfere with automated scripts
  • PAGER=cat ensures output is displayed directly without interactive pagers that would hang in non-interactive environments
  • timeout command prevents kubectl exec from hanging when psql tries to enter interactive mode
  • Avoiding -it flags in kubectl exec prevents forcing interactive/TTY mode for simple queries
  • These settings are essential for CI/CD, automated scripts, and remote execution scenarios