Skip to content

Conversation

@diego-feito-stori
Copy link

@diego-feito-stori diego-feito-stori commented Dec 2, 2025

Summary

This PR adds four major enhancements to pgcli:

  • Multiple -c/--command support: Execute multiple SQL commands sequentially from command line
  • -f/--file option: Execute SQL commands from one or more files
  • -y/--yes flag: Bypass confirmation prompts for destructive operations
  • .pgpass support with SSH tunnels: Properly use .pgpass authentication when connecting through SSH tunnels

Motivation

These features improve pgcli's scriptability and automation capabilities:

  • Multiple -c flags allow complex command sequences without temp files
  • -f flag enables SQL script execution similar to psql
  • -y flag removes interactive prompts for CI/CD pipelines
  • .pgpass SSH tunnel support fixes authentication issues when using tunnels

Changes

1. Multiple -c/--command parameters (PR #1530)

  • Modified main.py to accept multiple -c options
  • Commands execute sequentially
  • Exit after all commands complete
  • Full test coverage with behave tests

2. -f/--file option (PR #1531)

  • Added --file option to execute SQL from files
  • Supports multiple -f options
  • Compatible with -c flag (files execute after commands)
  • Comprehensive test suite included

3. -y/--yes flag (PR #1533)

  • Added --yes flag to force destructive commands
  • Skips confirmation prompts for DROP, TRUNCATE, etc.
  • Essential for automated scripts
  • Tested with behave feature tests

4. .pgpass SSH tunnel support (PR #1534)

  • Fixed issue where SSH tunnels broke .pgpass lookup
  • Uses PostgreSQL's host + hostaddr parameters correctly
  • host preserves original hostname for .pgpass
  • hostaddr uses 127.0.0.1 for actual connection
  • Updated pgexecute.py to support hostaddr parameter
  • All existing tests pass (2551 passed, 119 skipped)

Test Plan

All features include comprehensive tests:

  • ✅ Unit tests in tests/test_main.py
  • ✅ Behave integration tests for each feature
  • ✅ SSH tunnel tests in tests/test_ssh_tunnel.py
  • ✅ All existing tests continue to pass

Files Modified

  • pgcli/main.py: CLI argument parsing and execution logic
  • pgcli/pgexecute.py: SSH tunnel connection handling
  • tests/: Comprehensive test coverage for all features
  • changelog.rst: Documented changes
  • pyproject.toml: Updated dependencies

Version

Bumped to 4.3.4

🤖 Generated with Claude Code

diego-feito-stori and others added 15 commits November 19, 2025 16:19
   Implements the -f/--file command-line option similar to psql's behavior.
   This allows executing SQL commands from a file and then exiting.

   Features:
   - Support for -f and --file (short and long forms)
   - Multiple files can be specified (-f file1 -f file2)
   - Can be combined with -c option (-c commands execute first, then -f files)
   - Pager is disabled in file mode (consistent with -c behavior)
   - Comprehensive BDD tests added for all scenarios
   - Version bumped to 4.3.1
When using SSH tunnels, PostgreSQL's .pgpass file was not being used
because the connection was using '127.0.0.1' as the hostname instead
of the original database hostname.

This change preserves the original hostname using PostgreSQL's host/
hostaddr parameters:
- host: original database hostname (used for .pgpass lookup and SSL)
- hostaddr: 127.0.0.1 (actual connection endpoint via SSH tunnel)

Additionally:
- Fix connect_uri() to pass DSN parameter for proper .pgpass handling
- Add paramiko version constraint to avoid DSSKey compatibility issues
- Add SSH tunnel configuration options (ssh_config_file, allow_agent)
- Preserve hostaddr parameter when using DSN connections
- Add comprehensive test coverage for .pgpass + SSH tunnel scenarios

Fixes: SSH tunnel connections now work seamlessly with .pgpass files
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants