Skip to content

Commit e2d7771

Browse files
timhaley94brianc
authored andcommitted
Url parsing example (brianc#19)
* Added URL parsing example * Typos and cleanup * Last typo
1 parent 8c42c41 commit e2d7771

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,40 @@ var PgNativeClient = require('pg-native')
4646
var pgNativePool = new Pool({ Client: PgNativeClient })
4747
```
4848

49+
##### Note:
50+
The Pool constructor does not support passing a Database URL as the parameter. To use pg-pool on heroku, for example, you need to parse the URL into a config object. Here is an example of how to parse a Database URL.
51+
52+
```js
53+
const Pool = require('pg-pool');
54+
const url = require('url')
55+
56+
const params = url.parse(process.env.DATABASE_URL);
57+
const auth = params.auth.split(':');
58+
59+
const config = {
60+
user: auth[0],
61+
password: auth[1],
62+
host: params.hostname,
63+
port: params.port,
64+
database: params.pathname.split('/')[1],
65+
ssl: true
66+
};
67+
68+
const pool = Pool(config);
69+
70+
/*
71+
Transforms, 'progres://DBuser:secret@DBHost:#####/myDB', into
72+
config = {
73+
user: 'DBuser',
74+
password: 'secret',
75+
host: 'DBHost',
76+
port: '#####',
77+
database: 'myDB',
78+
ssl: true
79+
}
80+
*/
81+
```
82+
4983
### acquire clients with a promise
5084

5185
pg-pool supports a fully promise-based api for acquiring clients

0 commit comments

Comments
 (0)