You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you pass an object to `client.query` and the object has a `.submit` function on it, the client will pass it's PostgreSQL server connection to the object and delegate query dispatching to the supplied object. This is an advanced feature mostly intended for library authors. It is incidentally also currently how the callback and promise based queries above are handled internally, but this is subject to change. It is also how [pg-cursor](https://github.com/brianc/node-pg-cursor) and [pg-query-stream](https://github.com/brianc/node-pg-query-stream) work.
135
139
136
140
```js
137
-
import { Query } from'pg'
141
+
importpgfrom'pg'
142
+
const { Query } = pg
138
143
constquery=newQuery('select $1::text as name', ['brianc'])
@@ -100,7 +102,8 @@ Acquires a client from the pool.
100
102
- If the pool is 'full' and all clients are currently checked out will wait in a FIFO queue until a client becomes available by it being released back to the pool.
101
103
102
104
```js
103
-
import { Pool } from'pg'
105
+
importpgfrom'pg'
106
+
const { Pool } = pg
104
107
105
108
constpool=newPool()
106
109
@@ -118,7 +121,8 @@ Client instances returned from `pool.connect` will have a `release` method which
118
121
The `release` method on an acquired client returns it back to the pool. If you pass a truthy value in the `destroy` parameter, instead of releasing the client to the pool, the pool will be instructed to disconnect and destroy this client, leaving a space within itself for a new client.
119
122
120
123
```js
121
-
import { Pool } from'pg'
124
+
importpgfrom'pg'
125
+
const { Pool } = pg
122
126
123
127
constpool=newPool()
124
128
@@ -130,7 +134,8 @@ client.release()
130
134
```
131
135
132
136
```js
133
-
import { Pool } from'pg'
137
+
importpgfrom'pg'
138
+
const { Pool } = pg
134
139
135
140
constpool=newPool()
136
141
assert(pool.totalCount===0)
@@ -163,7 +168,8 @@ Calling `pool.end` will drain the pool of all active clients, disconnect them, a
163
168
164
169
```js
165
170
// again both promises and callbacks are supported:
Copy file name to clipboardExpand all lines: docs/pages/apis/result.mdx
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,8 @@ Every result will have a rows array. If no rows are returned the array will be e
18
18
Every result will have a fields array. This array contains the `name` and `dataTypeID` of each field in the result. These fields are ordered in the same order as the columns if you are using `arrayMode` for the query:
Copy file name to clipboardExpand all lines: docs/pages/features/connecting.mdx
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,8 @@ title: Connecting
7
7
node-postgres uses the same [environment variables](https://www.postgresql.org/docs/9.1/static/libpq-envars.html) as libpq and psql to connect to a PostgreSQL server. Both individual clients & pools will use these environment variables. Here's a tiny program connecting node.js to the PostgreSQL server:
8
8
9
9
```js
10
-
import { Pool, Client } from'pg'
10
+
importpgfrom'pg'
11
+
const { Pool, Client } = pg
11
12
12
13
// pools will use environment variables
13
14
// for connection information
@@ -54,7 +55,8 @@ PGPORT=5432
54
55
node-postgres also supports configuring a pool or client programmatically with connection information. Here's our same script from above modified to use programmatic (hard-coded in this case) values. This can be useful if your application already has a way to manage config values or you don't want to use environment variables.
55
56
56
57
```js
57
-
import { Pool, Client } from'pg'
58
+
importpgfrom'pg'
59
+
const { Pool, Client } = pg
58
60
59
61
constpool=newPool({
60
62
user:'dbuser',
@@ -84,7 +86,8 @@ await client.end()
84
86
Many cloud providers include alternative methods for connecting to database instances using short-lived authentication tokens. node-postgres supports dynamic passwords via a callback function, either synchronous or asynchronous. The callback function must resolve to a string.
85
87
86
88
```js
87
-
import { Pool } from'pg'
89
+
importpgfrom'pg'
90
+
const { Pool } = pg
88
91
import { RDS } from'aws-sdk'
89
92
90
93
constsignerOptions= {
@@ -116,7 +119,8 @@ const pool = new Pool({
116
119
Connections to unix sockets can also be made. This can be useful on distros like Ubuntu, where authentication is managed via the socket connection instead of a password.
117
120
118
121
```js
119
-
import { Client } from'pg'
122
+
importpgfrom'pg'
123
+
const { Client } = pg
120
124
client =newClient({
121
125
host:'/cloudsql/myproject:zone:mydb',
122
126
user:'username',
@@ -130,7 +134,8 @@ client = new Client({
130
134
You can initialize both a pool and a client with a connection string URI as well. This is common in environments like Heroku where the database connection string is supplied to your application dyno through an environment variable. Connection string parsing brought to you by [pg-connection-string](https://github.com/brianc/node-postgres/tree/master/packages/pg-connection-string).
Copy file name to clipboardExpand all lines: docs/pages/features/pooling.mdx
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,8 @@ The client pool allows you to have a reusable pool of clients you can check out,
28
28
### Checkout, use, and return
29
29
30
30
```js
31
-
import { Pool } from'pg'
31
+
importpgfrom'pg'
32
+
const { Pool } = pg
32
33
33
34
constpool=newPool()
34
35
@@ -60,7 +61,8 @@ client.release()
60
61
If you don't need a transaction or you just need to run a single query, the pool has a convenience method to run a query on any available client in the pool. This is the preferred way to query with node-postgres if you can as it removes the risk of leaking a client.
To shut down a pool call `pool.end()` on the pool. This will wait for all checked-out clients to be returned and then shut down all the clients and the pool timers.
0 commit comments