Perform command line SQL queries on a PostgreSQL database.
This is port of the old db_request.py tool to python 3. It uses psycopg2 to access a postgreSQL database.
Features:
COMMAND must be a valid SQL statement or one these strings:
If no SQL-statement is given, the program expects to read a statement from standard input.
The database connection arguments can be specified with a profile (option --profile) or separately with command line options --user, --password, --instance, --server and --port. Missing connection arguments are requested interactively on the command line. Command line options take precedence over the specifed profile.
Several output formats are supported:
The program can cache data from sql requests in files in a cache directory, which can be specifed by the command line options --cache and --cachemode or envorinment variables PG_REQUEST_CACHE and PG_REQUEST_CACHEMODE.
The cache directory contains a file "directory.json", which is a JSON file than maps a connection data string and a sql statement to a file and a number of json data files, one for each sql query. The data files are named HASH1-HASH2.json where HASH1 is an MD5 hash on the connection data string and HASH2 is an MD5 hash on the sql query string.
There are three cachemodes:
usage: pg_request.py [OPTIONS] COMMAND
-h, --help | show this help message and exit |
--doc | Create online help in restructured text format. Use "pg_request.py --doc | rst2html" for creation of html help. |
--man | Display a man page |
-u USERNAME, --user USERNAME | |
Set the USERNAME. | |
--password PASSWORD | |
Set the PASSWORD. | |
-s SERVER, --server SERVER | |
Set database SERVER. | |
-p SERVERPORT, --port SERVERPORT | |
Set database SERVERPORT. | |
-i INSTANCE, --instance INSTANCE | |
Set name of database INSTANCE. | |
-x PROFILE, --profile PROFILE | |
Use a PROFILE with predefined database connection parameters, one of 'devices2', 'devices2015'. 'devices2015' is the default if this option is not given. | |
-X, --no-profile | |
Do not use the default profile. You must specify the connection parameters by separate command line options or interactively in this case. | |
-o FORMAT, --format FORMAT | |
Define the output FORMAT, known: 'csv', 'csv-quoted', 'default', 'json', 'json-full', 'python', 'table'. | |
--header | Add column header. |
--profiles | List properties of known connection profiles. |
--csv | Define output format to csv. |
--csv-quoted | Define output format to csv-quoted. |
--python | Define output format to python. |
--table | Define output format to table. |
--json | Define output format to json. |
--json-full | Define output format to json with a full key-value structure. |
-c CACHEDIRECTORY, --cache CACHEDIRECTORY | |
Define a CACHEDIRECTORY | |
--cachemode CACHEMODE | |
Define the CACHEMODE, allowed : 'offline', 'cache' (default) or 'update' | |
-v, --verbose | Print some diagnostic to stderr. |