|
| 1 | +pg-connection-string |
| 2 | +==================== |
| 3 | + |
| 4 | +[](https://nodei.co/npm/pg-connection-string/) |
| 5 | + |
| 6 | +[](https://travis-ci.org/iceddev/pg-connection-string) |
| 7 | +[](https://coveralls.io/github/iceddev/pg-connection-string?branch=master) |
| 8 | + |
| 9 | +Functions for dealing with a PostgresSQL connection string |
| 10 | + |
| 11 | +`parse` method taken from [node-postgres](https://github.com/brianc/node-postgres.git) |
| 12 | +Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com) |
| 13 | +MIT License |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```js |
| 18 | +var parse = require('pg-connection-string').parse; |
| 19 | + |
| 20 | +var config = parse('postgres://someuser:somepassword@somehost:381/somedatabase') |
| 21 | +``` |
| 22 | + |
| 23 | +The resulting config contains a subset of the following properties: |
| 24 | + |
| 25 | +* `host` - Postgres server hostname or, for UNIX doamain sockets, the socket filename |
| 26 | +* `port` - port on which to connect |
| 27 | +* `user` - User with which to authenticate to the server |
| 28 | +* `password` - Corresponding password |
| 29 | +* `database` - Database name within the server |
| 30 | +* `client_encoding` - string encoding the client will use |
| 31 | +* `ssl`, either a boolean or an object with properties |
| 32 | + * `cert` |
| 33 | + * `key` |
| 34 | + * `ca` |
| 35 | +* any other query parameters (for example, `application_name`) are preserved intact. |
| 36 | + |
| 37 | +## Connection Strings |
| 38 | + |
| 39 | +The short summary of acceptable URLs is: |
| 40 | + |
| 41 | + * `socket:<path>?<query>` - UNIX domain socket |
| 42 | + * `postgres://<user>:<password>@<host>:<port>/<database>?<query>` - TCP connection |
| 43 | + |
| 44 | +But see below for more details. |
| 45 | + |
| 46 | +### UNIX Domain Sockets |
| 47 | + |
| 48 | +When user and password are not given, the socket path follows `socket:`, as in `socket:/var/run/pgsql`. |
| 49 | +This form can be shortened to just a path: `/var/run/pgsql`. |
| 50 | + |
| 51 | +When user and password are given, they are included in the typical URL positions, with an empty `host`, as in `socket://user:pass@/var/run/pgsql`. |
| 52 | + |
| 53 | +Query parameters follow a `?` character, including the following special query parameters: |
| 54 | + |
| 55 | + * `db=<database>` - sets the database name (urlencoded) |
| 56 | + * `encoding=<encoding>` - sets the `client_encoding` property |
| 57 | + |
| 58 | +### TCP Connections |
| 59 | + |
| 60 | +TCP connections to the Postgres server are indicated with `pg:` or `postgres:` schemes (in fact, any scheme but `socket:` is accepted). |
| 61 | +If username and password are included, they should be urlencoded. |
| 62 | +The database name, however, should *not* be urlencoded. |
| 63 | + |
| 64 | +Query parameters follow a `?` character, including the following special query parameters: |
| 65 | + * `host=<host>` - sets `host` property, overriding the URL's host |
| 66 | + * `encoding=<encoding>` - sets the `client_encoding` property |
| 67 | + * `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly |
| 68 | + * `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert` |
| 69 | + * `sslkey=<filename>` - reads data from the given file and includes the result as `ssl.key` |
| 70 | + * `sslrootcert=<filename>` - reads data from the given file and includes the result as `ssl.ca` |
| 71 | + |
| 72 | +A bare relative URL, such as `salesdata`, will indicate a database name while leaving other properties empty. |
0 commit comments