Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4062834
adding -c / --command parameter to pgcli w/tests
diego-feito-stori Nov 19, 2025
d607e17
adding support for multiples -c / --command parameter to pgcli w/tests
diego-feito-stori Nov 19, 2025
309ffc2
adding support for multiples -c / --command parameter to pgcli w/tests
DiegoDAF Nov 20, 2025
5fb4d38
Add -f/--file option to execute SQL commands from file
DiegoDAF Nov 25, 2025
f59ab57
adding -y / --yes parameter to avoid questions in pgcli w/tests
DiegoDAF Nov 26, 2025
41f8bc8
adding tests for -y / --yes
DiegoDAF Nov 26, 2025
6bb72d7
Enable .pgpass support for SSH tunnel connections
DiegoDAF Nov 27, 2025
e033c1c
adding missing changelog file
DiegoDAF Nov 27, 2025
b2ba3ec
fixing dependencies
DiegoDAF Nov 27, 2025
b9b206b
Downgrade version from 4.3.3 to 4.3.0
diego-feito-stori Nov 27, 2025
57e174e
Merge PR #1530: Add support for multiple -c/--command parameters
DiegoDAF Nov 27, 2025
8a92832
Merge PR #1531: Add -f/--file option to execute SQL from file
DiegoDAF Nov 27, 2025
0dd67b7
Merge PR #1533: Add -y/--yes flag to bypass confirmation prompts
DiegoDAF Nov 27, 2025
5cb0319
Merge PR #1534: Enable .pgpass support for SSH tunnel connections
DiegoDAF Nov 27, 2025
479f55b
Bump version to 4.3.4
DiegoDAF Dec 2, 2025
dde266e
Add --tuples-only (-t) option to print rows without extra output
DiegoDAF Dec 2, 2025
1d30323
Add BDD tests for --tuples-only option
DiegoDAF Dec 2, 2025
24488f4
Fix duplicate step definitions and update version to 4.3.5
DiegoDAF Dec 2, 2025
7833779
Update changelog for --tuples-only feature
DiegoDAF Dec 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ venv/
.ropeproject/
uv.lock

pgcli/__init__.py
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Contributors:
* Jay Knight (jay-knight)
* fbdb
* Charbel Jacquin (charbeljc)
* Diego

Creator:
--------
Expand Down
27 changes: 27 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,39 @@ Upcoming (TBD)

Features:
---------
* Add support for `tuples-only` option to print rows without extra output.
* Command line option `-t` or `--tuples-only`.
* Without value, defaults to `csv-noheader` format.
* Optionally specify a table format (e.g., `-t minimal`).
* Suppresses status messages (SELECT X) and timing information.
* Similar to psql's `-t` flag, useful for scripting and automation.
* Add support for `init-command` to run when the connection is established.
* Command line option `--init-command`
* Provide `init-command` in the config file
* Support dsn specific init-command in the config file
* Add suggestion when setting the search_path
* Allow per dsn_alias ssh tunnel selection
* Add support for `single-command` to run a SQL command and exit.
* Command line option `-c` or `--command`.
* You can specify multiple times.
* Add support for `file` to execute commands from a file and exit.
* Command line option `-f` or `--file`.
* You can specify multiple times.
* Similar to psql's `-f` option.
* Add support for forcing destructive commands without confirmation.
* Command line option `-y` or `--yes`.
* Skips the destructive command confirmation prompt when enabled.
* Useful for automated scripts and CI/CD pipelines.
* Add hostaddr to handle .pgpass with ssh tunnels

Documentation:
--------------

* Document previously undocumented table formats in config file:
* `csv-noheader` - CSV format without headers
* `tsv_noheader` - TSV format without headers
* `csv-tab-noheader` - Alias for tsv_noheader
* `minimal` - Aligned columns without headers or borders

Internal:
---------
Expand Down
2 changes: 1 addition & 1 deletion pgcli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.3.0"
__version__ = "4.3.5"
141 changes: 130 additions & 11 deletions pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

OutputSettings = namedtuple(
"OutputSettings",
"table_format dcmlfmt floatfmt column_date_formats missingval expanded max_width case_function style_output max_field_width",
"table_format dcmlfmt floatfmt column_date_formats missingval expanded max_width case_function style_output max_field_width tuples_only",
)
OutputSettings.__new__.__defaults__ = (
None,
Expand All @@ -130,6 +130,7 @@
lambda x: x,
None,
DEFAULT_MAX_FIELD_WIDTH,
False,
)


Expand Down Expand Up @@ -179,18 +180,21 @@ def __init__(
application_name="pgcli",
single_connection=False,
less_chatty=None,
tuples_only=None,
prompt=None,
prompt_dsn=None,
auto_vertical_output=False,
warn=None,
ssh_tunnel_url: Optional[str] = None,
log_file: Optional[str] = None,
force_destructive: bool = False,
):
self.force_passwd_prompt = force_passwd_prompt
self.never_passwd_prompt = never_passwd_prompt
self.pgexecute = pgexecute
self.dsn_alias = None
self.watch_command = None
self.force_destructive = force_destructive

# Load config.
c = self.config = get_config(pgclirc_file)
Expand Down Expand Up @@ -235,7 +239,13 @@ def __init__(

self.min_num_menu_lines = c["main"].as_int("min_num_menu_lines")
self.multiline_continuation_char = c["main"]["multiline_continuation_char"]
self.table_format = c["main"]["table_format"]
# Override table_format if tuples_only is specified
if tuples_only:
self.table_format = tuples_only
self.tuples_only = True
else:
self.table_format = c["main"]["table_format"]
self.tuples_only = False
self.syntax_style = c["main"]["syntax_style"]
self.cli_style = c["colors"]
self.wider_completion_menu = c["main"].as_bool("wider_completion_menu")
Expand Down Expand Up @@ -484,7 +494,10 @@ def execute_from_file(self, pattern, **_):
):
message = "Destructive statements must be run within a transaction. Command execution stopped."
return [(None, None, None, message)]
destroy = confirm_destructive_query(query, self.destructive_warning, self.dsn_alias)
if self.force_destructive:
destroy = True
else:
destroy = confirm_destructive_query(query, self.destructive_warning, self.dsn_alias)
if destroy is False:
message = "Wise choice. Command execution stopped."
return [(None, None, None, message)]
Expand Down Expand Up @@ -594,7 +607,8 @@ def connect_uri(self, uri):
kwargs = conninfo_to_dict(uri)
remap = {"dbname": "database", "password": "passwd"}
kwargs = {remap.get(k, k): v for k, v in kwargs.items()}
self.connect(**kwargs)
# Pass the original URI as dsn parameter for .pgpass support with SSH tunnels
self.connect(dsn=uri, **kwargs)

def connect(self, database="", host="", user="", port="", passwd="", dsn="", **kwargs):
# Connect to the database.
Expand Down Expand Up @@ -657,6 +671,14 @@ def should_ask_for_password(exc):
break

if self.ssh_tunnel_url:
if not SSH_TUNNEL_SUPPORT:
click.secho(
"SSH tunnel requires sshtunnel package. Install it with: pip install sshtunnel",
err=True,
fg="red",
)
sys.exit(1)

# We add the protocol as urlparse doesn't find it by itself
if "://" not in self.ssh_tunnel_url:
self.ssh_tunnel_url = f"ssh://{self.ssh_tunnel_url}"
Expand All @@ -667,6 +689,9 @@ def should_ask_for_password(exc):
"remote_bind_address": (host, int(port or 5432)),
"ssh_address_or_host": (tunnel_info.hostname, tunnel_info.port or 22),
"logger": self.logger,
"ssh_config_file": "~/.ssh/config", # Use SSH config for host settings
"allow_agent": True, # Allow SSH agent for authentication
"compression": False, # Disable compression for better performance
}
if tunnel_info.username:
params["ssh_username"] = tunnel_info.username
Expand All @@ -687,11 +712,16 @@ def should_ask_for_password(exc):
self.logger.handlers = logger_handlers

atexit.register(self.ssh_tunnel.stop)
host = "127.0.0.1"
# Preserve original host for .pgpass lookup and SSL certificate verification
# Use hostaddr to specify the actual connection endpoint (SSH tunnel)
hostaddr = "127.0.0.1"
port = self.ssh_tunnel.local_bind_ports[0]

if dsn:
dsn = make_conninfo(dsn, host=host, port=port)
dsn = make_conninfo(dsn, host=host, hostaddr=hostaddr, port=port)
else:
# For non-DSN connections, pass hostaddr via kwargs
kwargs["hostaddr"] = hostaddr

# Attempt to connect to the database.
# Note that passwd may be empty on the first attempt. If connection
Expand Down Expand Up @@ -792,7 +822,10 @@ def execute_command(self, text, handle_closed_connection=True):
):
click.secho("Destructive statements must be run within a transaction.")
raise KeyboardInterrupt
destroy = confirm_destructive_query(text, self.destructive_warning, self.dsn_alias)
if self.force_destructive:
destroy = True
else:
destroy = confirm_destructive_query(text, self.destructive_warning, self.dsn_alias)
if destroy is False:
click.secho("Wise choice!")
raise KeyboardInterrupt
Expand Down Expand Up @@ -850,7 +883,7 @@ def execute_command(self, text, handle_closed_connection=True):
except KeyboardInterrupt:
pass

if self.pgspecial.timing_enabled:
if self.pgspecial.timing_enabled and not self.tuples_only:
# Only add humanized time display if > 1 second
if query.total_time > 1:
print(
Expand Down Expand Up @@ -911,6 +944,44 @@ def _check_ongoing_transaction_and_allow_quitting(self):
def run_cli(self):
logger = self.logger

# Handle command mode (-c flag) and/or file mode (-f flag)
# Similar to psql behavior: execute commands/files and exit
has_commands = hasattr(self, 'commands') and self.commands
has_input_files = hasattr(self, 'input_files') and self.input_files

if has_commands or has_input_files:
try:
# Execute -c commands first, if any
if has_commands:
for command in self.commands:
logger.debug("Running command: %s", command)
self.handle_watch_command(command)

# Then execute commands from files, if provided
# Multiple -f options are executed sequentially
if has_input_files:
for input_file in self.input_files:
logger.debug("Reading commands from file: %s", input_file)
with open(input_file, 'r', encoding='utf-8') as f:
file_content = f.read()

# Execute the entire file content as a single command
# This matches psql behavior where the file is treated as one unit
if file_content.strip():
logger.debug("Executing commands from file: %s", input_file)
self.handle_watch_command(file_content)

except PgCliQuitError:
# Normal exit from quit command
sys.exit(0)
except Exception as e:
logger.error("Error executing command: %s", e)
logger.error("traceback: %r", traceback.format_exc())
click.secho(str(e), err=True, fg="red")
sys.exit(1)
# Exit successfully after executing all commands
sys.exit(0)

history_file = self.config["main"]["history_file"]
if history_file == "default":
history_file = config_location() + "history"
Expand Down Expand Up @@ -1135,6 +1206,7 @@ def _evaluate_command(self, text):
case_function=(self.completer.case if self.settings["case_column_headers"] else lambda x: x),
style_output=self.style_output,
max_field_width=self.max_field_width,
tuples_only=self.tuples_only,
)
execution = time() - start
formatted = format_output(title, cur, headers, status, settings, self.explain_mode)
Expand Down Expand Up @@ -1278,7 +1350,10 @@ def is_too_tall(self, lines):
return len(lines) >= (self.prompt_app.output.get_size().rows - 4)

def echo_via_pager(self, text, color=None):
if self.pgspecial.pager_config == PAGER_OFF or self.watch_command:
# Disable pager for -c/--command mode, -f/--file mode, and \watch command
has_commands = hasattr(self, 'commands') and self.commands
has_input_files = hasattr(self, 'input_files') and self.input_files
if self.pgspecial.pager_config == PAGER_OFF or self.watch_command or has_commands or has_input_files:
click.echo(text, color=color)
elif self.pgspecial.pager_config == PAGER_LONG_OUTPUT and self.table_format != "csv":
lines = text.split("\n")
Expand Down Expand Up @@ -1381,6 +1456,15 @@ def echo_via_pager(self, text, color=None):
default=False,
help="Skip intro on startup and goodbye on exit.",
)
@click.option(
"-t",
"--tuples-only",
"tuples_only",
is_flag=False,
flag_value="csv-noheader",
default=None,
help="Print rows only (default: csv-noheader). Optionally specify a format (e.g., -t minimal).",
)
@click.option("--prompt", help='Prompt format (Default: "\\u@\\h:\\d> ").')
@click.option(
"--prompt-dsn",
Expand Down Expand Up @@ -1426,6 +1510,29 @@ def echo_via_pager(self, text, color=None):
type=str,
help="SQL statement to execute after connecting.",
)
@click.option(
"-c",
"--command",
"commands",
multiple=True,
help="run command (SQL or internal) and exit. Multiple -c options are allowed.",
)
@click.option(
"-f",
"--file",
"input_files",
multiple=True,
type=click.Path(exists=True, readable=True, dir_okay=False),
help="execute commands from file, then exit. Multiple -f options are allowed.",
)
@click.option(
"-y",
"--yes",
"force_destructive",
is_flag=True,
default=False,
help="Force destructive commands without confirmation prompt.",
)
@click.argument("dbname", default=lambda: None, envvar="PGDATABASE", nargs=1)
@click.argument("username", default=lambda: None, envvar="PGUSER", nargs=1)
def cli(
Expand All @@ -1444,6 +1551,7 @@ def cli(
row_limit,
application_name,
less_chatty,
tuples_only,
prompt,
prompt_dsn,
list_databases,
Expand All @@ -1454,6 +1562,9 @@ def cli(
ssh_tunnel: str,
init_command: str,
log_file: str,
commands: tuple,
input_files: tuple,
force_destructive: bool,
):
if version:
print("Version:", __version__)
Expand Down Expand Up @@ -1506,14 +1617,22 @@ def cli(
application_name=application_name,
single_connection=single_connection,
less_chatty=less_chatty,
tuples_only=tuples_only,
prompt=prompt,
prompt_dsn=prompt_dsn,
auto_vertical_output=auto_vertical_output,
warn=warn,
ssh_tunnel_url=ssh_tunnel,
log_file=log_file,
force_destructive=force_destructive,
)

# Store commands for -c option (can be multiple)
pgcli.commands = commands if commands else None

# Store file paths for -f option (can be multiple)
pgcli.input_files = input_files if input_files else None

# Choose which ever one has a valid value.
if dbname_opt and dbname:
# work as psql: when database is given as option and argument use the argument as user
Expand Down Expand Up @@ -1893,8 +2012,8 @@ def format_status(cur, status):

output = itertools.chain(output, formatted)

# Only print the status if it's not None
if status:
# Only print the status if it's not None and tuples_only is not enabled
if status and not settings.tuples_only:
output = itertools.chain(output, [format_status(cur, status)])

return output
Expand Down
3 changes: 3 additions & 0 deletions pgcli/pgclirc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ show_bottom_toolbar = True
# textile, moinmoin, jira, vertical, tsv, csv, sql-insert, sql-update,
# sql-update-1, sql-update-2 (formatter with sql-* prefix can format query
# output to executable insertion or updating sql).
# Additional formats: minimal (aligned columns without headers or borders),
# csv-noheader (CSV without headers), tsv_noheader (TSV without headers),
# csv-tab-noheader (same as tsv_noheader).
# Recommended: psql, fancy_grid and grid.
table_format = psql

Expand Down
6 changes: 5 additions & 1 deletion pgcli/pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ def connect(
new_params.update(kwargs)

if new_params["dsn"]:
new_params = {"dsn": new_params["dsn"], "password": new_params["password"]}
# Preserve hostaddr when using DSN (needed for SSH tunnels with .pgpass)
preserved_params = {"dsn": new_params["dsn"], "password": new_params["password"]}
if "hostaddr" in new_params:
preserved_params["hostaddr"] = new_params["hostaddr"]
new_params = preserved_params

if new_params["password"]:
new_params["dsn"] = make_conninfo(new_params["dsn"], password=new_params.pop("password"))
Expand Down
Loading
Loading