pg_servicefile_util
v1.0.1
Published
CLI and library for utilizing Postgres Service Files
Downloads
6
Maintainers
Readme
pg_servicefile_util
CLI and library for utilizing Postgres Service Files
Install
CLI
npm install -g pg_servicefile_util
pg_servicefile_util --help
Library
npm install pg_servicefile_util
import {toConnectionUri, toEnv, getServiceConfig} from 'pg_servicefile_util'
CLI Usage
> pg_servicefile_util --help
pg_servicefile_util <service>
Get parameters for specified Postgres service from well-known service file locations.
Example locations:
./.pg_service.conf
~/.pg_service.conf
%APPDATA%\postgresql\.pg_service.conf
/etc/postgresql/.pg_service.conf
$PGSERVICEFILE
For more details, see:
https://www.postgresql.org/docs/current/libpq-pgservice.html
Positionals:
service Service name to use within service file [default: "postgres"]
Options:
--help Show help [boolean]
--version Show version number [boolean]
--uri Return service file parameters formatted as a connection uri [boolean]
--env Sets environmental variables from service file parameters (as their Postgres-equivalent) [boolean]
-p, --printenv Prints the Postgres-equivalent environmental variables formatted for passing to `eval` or `source` [boolean]
File Locations
- Windows
%APPDATA%\postgresql.pg_service.conf
- Unix
~/.pg_service.conf
- Local
./.pg_service.conf
- Environment
$PGSERVICEFILE
- System
$PGSYSCONFDIR
/pg_service.conf
Examples
This is an example .pg_service.conf
file (that should exist in one of the locations above)
[postgres]
host=localhost
port=5432
user=postgres
dbname=postgres
[sandbox]
host=sandbox.example.com
port=5435
user=sandbox
password=sandbox
dbname=postgres
Note: The service names don't have to be postgres
or sandbox
-- these are just examples. They can be anything you want (though, postgres
is the default service name when one isn't passed to the cli).
For more details, see libpq-connect.
Print connection uri string for postgres
service
pg_servicefile_util postgres
postgresql://postgres@localhost:5432/postgres
Print connection uri string for sandbox
service
pg_servicefile_util sandbox
postgresql://sandbox:[email protected]:5435/postgres
Set env variables for sandbox
service
pg_servicefile_util sandbox --env
echo $PGHOST # sandbox.example.com
Eval (or source) env variables for sandbox
service in current session
eval $(pg_servicefile_util sandbox --printenv)
echo $PGHOST # sandbox.example.com
Pass to psql
(and run sql query)
echo "SELECT 1" | psql $(pg_servicefile_util postgres)
?column?
----------
1
(1 row)
Api examples
import {toConnectionUri, toEnv, getServiceConfig} from 'pg_servicefile_util'
// TODO...
TODO
- [ ] Finish README
- [ ] Document support and extensions (e.g., "recursive" service name keys)
- [ ] Add examples of api usage for common pg clients
- [ ] Add support for an
init
command that will generate a.pg_service.conf
file - [ ] Default to using
postgres
(or possiblydefault
) as the default "service name" and allow the binary/cli to be run w/o any arguments!