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 one of these:
If no COMMAND is given, the program expects to read an SQL 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 or in single separate cache files.
When a cache directory shall be used, option '--cache' or environment variable PG_REQUEST_CACHE must be set to an existing directory.
The cache directory contains a file "directory.json", which is a JSON file that maps a connection data string to a dictionary. This dictionary maps sql statement to files. 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.
When a cache file shall be used, option '--cachefile' or environment variable PG_REQUEST_CACHEFILE must be set.
In this case the result of an SQL query is stored in a single JSON file.
The cache mode must be specified with option '--cachemode' or environment variable PG_REQUEST_CACHEMODE.
The following cache modes are defined:
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. |
--version | Display program version. |
--man | Display a man page |
-u, --user USERNAME | |
Set the USERNAME. | |
--password PASSWORD | |
Set the PASSWORD. | |
-s, --server SERVER | |
Set database SERVER. | |
-p, --port SERVERPORT | |
Set database SERVERPORT. | |
-i, --instance INSTANCE | |
Set name of database INSTANCE. | |
-x, --profile PROFILE | |
Use a PROFILE with predefined database connection parameters, one of 'devices2', 'devices2015', 'devices2015_sqlite'. '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. | |
-f, --file FILE | |
Set the filename of an sqlite database. When file is 'DATABASE.db', database schemas are loaded from files named DATABASE.SCHEMA.db when they are found in the same directory as FILE. | |
-o, --format FORMAT | |
Define the output FORMAT, known: 'csv', 'csv-quoted', 'default', 'json', 'json-full', 'python', 'raw', '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, --cache CACHEDIRECTORY | |
Define a CACHEDIRECTORY | |
-C, --cachefile CACHEFILE | |
Define a CACHEFILE | |
--cachemode CACHEMODE | |
Define the CACHEMODE, allowed : off, offline, cache, update. | |
-v, --verbose | Print some diagnostic to stderr. |
--exception | do not catch exceptions (for debugging). |