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
port?:number|number[], // default process.env.PGPORT; one value or one per host
17
17
database?:string, // default process.env.PGDATABASE || user
18
18
connectionString?:string, // e.g. postgres://user:password@host:5432/database
19
19
ssl?:any, // passed directly to node.TLSSocket, supports all tls.connect options
@@ -29,7 +29,8 @@ type Config = {
29
29
idle_in_transaction_session_timeout?:number, // number of milliseconds before terminating any session with an open idle transaction, default is no timeout
30
30
client_encoding?:string, // specifies the character set encoding that the database uses for sending data to the client
31
31
fallback_application_name?:string, // provide an application name to use if application_name is not set
32
-
options?:string// command-line options to be sent to the server
32
+
options?:string, // command-line options to be sent to the server
33
+
targetSessionAttrs?:'any'|'read-write'|'read-only'|'primary'|'standby'|'prefer-standby', // default 'any'; requires host to be an array
Copy file name to clipboardExpand all lines: docs/pages/features/connecting.mdx
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,62 @@ client = new Client({
129
129
})
130
130
```
131
131
132
+
## Multiple hosts
133
+
134
+
node-postgres supports connecting to multiple PostgreSQL hosts. Pass arrays to `host` and `port` to enable automatic failover — the client tries each host in order and uses the first one it can reach.
awaitclient.connect() // tries hosts left to right until one succeeds
148
+
```
149
+
150
+
You can also specify a different port for each host:
151
+
152
+
```js
153
+
constclient=newClient({
154
+
host: ['host-a.db.com', 'host-b.db.com'],
155
+
port: [5432, 5433],
156
+
database:'mydb',
157
+
})
158
+
```
159
+
160
+
Port rules (same as libpq):
161
+
-**one port** — reused for every host
162
+
-**one port per host** — each port is paired with the corresponding host by index
163
+
- any other combination throws at construction time
164
+
165
+
### target_session_attrs
166
+
167
+
Use `targetSessionAttrs` to control which host is accepted based on its role. This mirrors the [libpq `target_session_attrs`](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-TARGET-SESSION-ATTRS) option.
168
+
169
+
```js
170
+
constclient=newClient({
171
+
host: ['primary.db.com', 'replica.db.com'],
172
+
port:5432,
173
+
targetSessionAttrs:'read-write', // only connect to a writable primary
174
+
})
175
+
```
176
+
177
+
| Value | Accepted server |
178
+
|---|---|
179
+
|`any` (default) | any server |
180
+
|`read-write`| server where `transaction_read_only = off`|
181
+
|`read-only`| server where `transaction_read_only = on`|
182
+
|`primary`| server that is not in hot standby |
183
+
|`standby`| server that is in hot standby |
184
+
|`prefer-standby`| standby if available, otherwise any |
185
+
186
+
When all hosts are exhausted without finding a matching server, the client emits an error.
187
+
132
188
## Connection URI
133
189
134
190
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).
0 commit comments