Add new PostgreSQL meta-commands
main\dp, \ds, \dy), you must contribute to the pgspecial project rather than the pgcli repository.repository·main·Indexed 12 days ago
https://github.com/dbcli/pgcliA command-line interface (REPL) for PostgreSQL featuring auto-completion, syntax highlighting, and smart context-sensitive suggestions.
\dp, \ds, \dy), you must contribute to the pgspecial project rather than the pgcli repository.You can install pgcli using various package managers depending on your operating system.
pip install -U pgcliUsing Homebrew:
brew install pgclisudo apt-get install pgclipipx install pgcli
# or
uvx pgclipip install -U pgcliYou can start a pgcli session by providing a database name or a connection URI.
Connect to a local database by name:
pgcli local_databaseConnect using a full PostgreSQL URI, which allows specifying credentials, host, port, and SSL parameters:
pgcli postgresql://[user[:password@][netloc][:port][/dbname][?extra=value[&other=other-value]]]Example with SSL:
pgcli postgres://amjith:pa$$w0rd@example.com:5432/app_db?sslmode=verify-ca&sslrootcert=/myrootcertpgcli local_databaseYou can run pgcli inside a Docker container to avoid local installation.
docker build -t pgcli .docker run --rm -ti pgcli pgcli <ARGS>Use --net host to access a PostgreSQL server running on your host machine:
docker run --rm -ti --net host pgcli pgcli -h localhost fooBind the local socket to the container:
docker run --rm -ti -v /var/run/postgres:/var/run/postgres pgcli pgcli foodocker run --rm -ti --net host pgcli pgcli -h localhost fooTo develop pgcli and see changes immediately without re-installing, use uv to create a virtual environment and install the package in editable mode.
uv and create a virtual environment:cd pgcli
uv venv
source ./pgcli-dev/bin/activatepgcli in editable mode:uv pip install -e .By using the -e or --editable flag, any changes made to the source code are immediately reflected in the installed version of pgcli.
cd pgcli
uv venv
source ./pgcli-dev/bin/activate
uv pip install -e .Unit tests can be executed using pytest from the project root.
cd pgcli
pytestIntegration tests use behave and pytest. They are located in the tests directory and are configured via a behave.ini file.
uv pip install ".[dev]"postgres user, ensure the authentication method in pg_hba.conf is set to trust. If you modify pg_hba.conf, restart the service:sudo service postgresql restartNavigate to the tests directory and run behave:
cd pgcli/tests
behaveTo see stdout/stderr output during tests, use:
behave --no-captureNote: behave tests are currently incompatible with Windows due to pexpect limitations.
$ uv pip install ".[dev]"
$ cd pgcli/tests
$ behaveTo debug pgcli in Visual Studio Code, create a .vscode/launch.json file in the project root. This configuration launches the pgcli.main module and allows you to set environment variables for database connection testing.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "pgcli.main",
"justMyCode": false,
"console": "externalTerminal",
"env": {
"PGUSER": "postgres",
"PGPASS": "password",
"PGHOST": "localhost",
"PGPORT": "5432"
}
}
]
}The sqlcompletion module uses several namedtuple types to categorize what kind of completion should be suggested to the user based on the current SQL cursor position. Common suggestion types include:
Table, View, Schema, Database: For structural database objects.Column: For column names, often scoped to specific tables.Function: For database functions.Keyword: For SQL keywords (e.g., SELECT, FROM).Datatype: For PostgreSQL data types.Join, JoinCondition: For JOIN clauses and ON conditions.Alias: For table aliases.Special: For psql-style special commands (e.g., \d).Path: For file paths (e.g., after \i ).NamedQuery: For saved named queries.These types are returned by the suggest_type function to inform the UI which metadata to fetch and display.
pgcli can use PostgreSQL service files to manage connection parameters. The parse_service_info logic determines which service configuration to use based on the following priority and environment variables:
service argument or the PGSERVICE environment variable.PGSERVICEFILE environment variable. If not set, pgcli looks for .pg_service.conf in these locations:PGSYSCONFDIR + \pg_service.conf.PGSYSCONFDIR + /.pg_service.conf (if PGSYSCONFDIR is set), otherwise defaults to ~/.pg_service.conf.If a valid service and file are found, the function returns the configuration object and the file path used.
Since pgcli uses libpq, it supports standard PostgreSQL environment variables for connection and SSL configuration.
PGHOSTPGPORTPGUSERPGPASSWORDPGDATABASETo connect via SSL, export the following variables:
export PGSSLMODE="verify-full"
export PGSSLCERT="/your-path-to-certs/client.crt"
export PGSSLKEY="/your-path-to-keys/client.key"
export PGSSLROOTCERT="/your-path-to-ca/ca.crt"
pgcli -h localhost -p 5432 -U username postgresexport PGSSLMODE="verify-full"
export PGSSLCERT="/your-path-to-certs/client.crt"
export PGSSLKEY="/your-path-to-keys/client.key"
export PGSSLROOTCERT="/your-path-to-ca/ca.crt"
pgcli -h localhost -p 5432 -U username postgresWhen connecting to virtual databases like PgBouncer, standard psycopg cursors may raise ProtocolViolation errors. PGExecute uses ProtocolSafeCursor to wrap and suppress these errors.
Instead of raising an exception that crashes the session, ProtocolSafeCursor:
protocol_error flag.fetchone() or fetchall() calls.__iter__ to stop immediately.