Getting started
Installation
$ npm install postgres
Usage
Create your sql
database instance
import postgres from 'postgres'
const sql = postgres({ })
export default sql
Simply import for use elsewhere
import sql from './db.js'
async function getUsersOver(age) {
const users = await sql`
select
name,
age
from users
where age > ${ age }
`
return users
}
async function insertUser({ name, age }) {
const users = sql`
insert into users
(name, age)
values
(${ name }, ${ age })
returning name, age
`
return users
}
Table of Contents
Connection
postgres([url], [options])
You can use either a postgres://
url connection string or the options to define your database connection properties. Options in the object will override any present in the url. Options will fall back to the same environment variables as psql.
const sql = postgres('postgres://username:password@host:port/database', {
host : '',
port : 5432,
database : '',
username : '',
password : '',
...and more
})
More options can be found in the Connection details section.
Queries
await sql`...` -> Result[]
Postgres.js utilizes Tagged template functions to process query parameters before interpolation. Using tagged template literals benefits developers by:
- Enforcing safe query generation
- Giving the
sql``
function powerful utility and query building features.
Any generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholder $1, $2, ...
. The parameters are then sent separately to the database which handles escaping & casting.
All queries will return a Result
array, with objects mapping column names to each row.
const xs = await sql`
insert into users (
name, age
) values (
'Murray', 68
)
returning *
`
Please note that queries are first executed when awaited
β or manually by using .execute()
.
Query parameters
Parameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual. Dynamic queries and query building can be seen in the next section. // todo
const name = 'Mur'
, age = 60
const users = await sql`
select
name,
age
from users
where
name like ${ name + '%' }
and age > ${ age }
`
Be careful with quotation marks here. Because Postgres infers column types, you do not need to wrap your interpolated parameters in quotes like '${name}'
. This will cause an error because the tagged template replaces ${name}
with $1
in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see '$1'
and interpret it as a string as opposed to a parameter.
Dynamic column selection
const columns = ['name', 'age']
sql`
select
${ sql(columns) }
from users
`
select "name", "age" from users
Dynamic inserts
const user = {
name: 'Murray',
age: 68
}
sql`
insert into users ${
sql(user, 'name', 'age')
}
`
insert into users ("name", "age") values ($1, $2)
You can omit column names and simply execute sql(user)
to get all the fields from the object as columns. Be careful not to allow users to supply columns that you do not want to be inserted.
Multiple inserts in one query
If you need to insert multiple rows at the same time it's also much faster to do it with a single insert
. Simply pass an array of objects to sql()
.
const users = [{
name: 'Murray',
age: 68,
garbage: 'ignore'
},
{
name: 'Walter',
age: 80
}]
sql`insert into users ${ sql(users, 'name', 'age') }`
insert into users ("name", "age") values ($1, $2), ($3, $4)
sql`insert into users ${ sql(users) }`
insert into users ("name", "age") values ($1, $2), ($3, $4)
Dynamic columns in updates
This is also useful for update queries
const user = {
id: 1,
name: 'Murray',
age: 68
}
sql`
update users set ${
sql(user, 'name', 'age')
}
where user_id = ${ user.id }
`
update users set "name" = $1, "age" = $2 where user_id = $3
Dyanmic values and where in
Value lists can also be created dynamically, making where in
queries simple too.
const users = await sql`
select
*
from users
where age in ${ sql([68, 75, 23]) }
`
or
const [{ a, b, c }] => await sql`
select
*
from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
Building queries
Postgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.
It works by nesting sql``
fragments within other sql``
calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.
Partial queries
const olderThan = x => sql`and age > ${ x }`
const filterAge = true
sql`
select
*
from users
where name is not null ${
filterAge
? olderThan(50)
: sql``
}
`
select * from users where name is not null
select * from users where name is not null and age > 50
Dynamic filters
sql`
select
*
from users ${
id
? sql`where user_id = ${ id }`
: sql``
}
`
select * from users
select * from users where user_id = $1
SQL functions
Using keywords or calling functions dynamically is also possible by using sql``
fragments.
const date = null
sql`
update users set updated_at = ${ date || sql`now()` }
`
update users set updated_at = now()
Table names
Dynamic identifiers like table names and column names is also supported like so:
const table = 'users'
, column = 'id'
sql`
select ${ sql(column) } from ${ sql(table) }
`
select "id" from "users"
Advanced query methods
.cursor()
await sql``.cursor([rows = 1], [fn])
Use cursors if you need to throttle the amount of rows being returned from a query. You can use a cursor either as an async iterable or with a callback function. For a callback function new results won't be requested until the promise / async callback function has resolved.
callback function
await sql`
select
*
from generate_series(1,4) as x
`.cursor(async([row]) => {
await http.request('https://example.com/wat', { row })
}
for await...of
const cursor = sql`select * from generate_series(1,4) as x`.cursor()
for await (const [row] of cursor) {
await http.request('https://example.com/wat', { row })
}
A single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument to .cursor
:
await sql`
select
*
from generate_series(1,1000) as x
`.cursor(10, async rows => {
await Promise.all(rows.map(row =>
http.request('https://example.com/wat', { row })
))
}
If an error is thrown inside the callback function no more rows will be requested and the outer promise will reject with the thrown error.
You can close the cursor early either by calling break
in the for await...of
loop, or by returning the token sql.CLOSE
from the callback function.
await sql`
select * from generate_series(1,1000) as x
`.cursor(row => {
return Math.random() > 0.9 && sql.END
})
.forEach()
await sql``.forEach(fn)
If you want to handle rows returned by a query one by one, you can use .forEach
which returns a promise that resolves once there are no more rows.
await sql`
select created_at, name from events
`.forEach(row => {
})
describe
await sql``.describe([rows = 1], fn) -> Result[]
Rather than executing a given query, .describe
will return information utilized in the query process. This information can include the query identifier, column types, etc.
This is useful for debugging and analyzing your Postgres queries. Furthermore, .describe
will give you access to the final generated query string that would be executed.
Raw
sql``.raw()
Using .raw()
will return rows as an array with Buffer
values for each column, instead of objects.
This can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
File
await sql.file(path, [args], [options]) -> Result[]
Using a .sql
file for a query is also supported with optional parameters to use if the file includes $1, $2, etc
const result = await sql.file('query.sql', ['Murray', 68])
Canceling Queries in Progress
Postgres.js supports, canceling queries in progress. It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.
const query = sql`select pg_sleep 100`.execute()
setTimeout(() => query.cancel(), 100)
const result = await query
Unsafe raw string queries
Advanced unsafe use cases
await sql.unsafe(query, [args], [options]) -> Result[]
If you know what you're doing, you can use unsafe
to pass any string you'd like to postgres. Please note that this can lead to sql injection if you're not careful.
sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
Transactions
BEGIN / COMMIT await sql.begin([options = ''], fn) -> fn()
Use sql.begin
to start a new transaction. Postgres.js will reserve a connection for the transaction and supply a scoped sql
instance for all transaction uses in the callback function. sql.begin
will resolve with the returned value from the callback function.
BEGIN
is automatically sent with the optional options, and if anything fails ROLLBACK
will be called so the connection can be released and execution can continue.
const [user, account] = await sql.begin(async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
`
const [account] = await sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
`
return [user, account]
})
It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:
const result = await sql.begin(sql => [
sql`update ...`,
sql`update ...`,
sql`insert ...`
])
SAVEPOINT await sql.savepoint([name], fn) -> fn()
sql.begin('read write', async sql => {
const [user] = await sql`
insert into users (
name
) values (
'Murray'
)
`
const [account] = (await sql.savepoint(sql =>
sql`
insert into accounts (
user_id
) values (
${ user.user_id }
)
`
).catch(err => {
})) || []
return [user, account]
})
.then(([user, account]) => {
})
.catch(() => {
})
Do note that you can often achieve the same result using WITH
queries (Common Table Expressions) instead of using transactions.
Listen & notify
When you call .listen
, a dedicated connection will be created to ensure that you receive notifications in real-time. This connection will be used for any further calls to .listen
.
.listen
returns a promise which resolves once the LISTEN
query to Postgres completes, or if there is already a listener active.
await sql.listen('news', payload => {
const json = JSON.parse(payload)
console.log(json.this)
})
Notify can be done as usual in sql, or by using the sql.notify
method.
sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
Realtime subscribe
Postgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of insert
, update
and delete
operations.
NOTE To make this work you must create the proper publications in your database, enable logical replication by setting wal_level = logical
in postgresql.conf
and connect using either a replication or superuser.
Quick start
Create a publication (eg. in migration)
CREATE PUBLICATION alltables FOR ALL TABLES
Subscribe to updates
const sql = postgres({ publications: 'alltables' })
const { unsubscribe } = await sql.subscribe('insert:events', (row, { command, relation, key, old }) =>
)
Subscribe pattern
You can subscribe to specific operations, tables, or even rows with primary keys.
operation
:
schema
.
table
=
primary_key
operation
is one of * | insert | update | delete
and defaults to *
schema
defaults to public.
table
is a specific table name and defaults to *
primary_key
can be used to only subscribe to specific rows
Examples
sql.subscribe('*', () => )
sql.subscribe('insert', () => )
sql.subscribe('*:users', () => )
sql.subscribe('delete:users', () => )
sql.subscribe('update:users=1', () => )
Numbers, bigint, numeric
Number
in javascript is only able to represent 253-1 safely which means that types in PostgreSQLs like bigint
and numeric
won't fit into Number
.
Since Node.js v10.4 we can use BigInt
to match the PostgreSQL type bigint
which is returned for eg. count(*)
. Unfortunately, it doesn't work with JSON.stringify
out of the box, so Postgres.js will return it as a string.
If you want to use BigInt
you can add this custom type:
const sql = postgres({
types: {
bigint: postgres.BigInt
}
})
There is currently no guaranteed way to handle numeric / decimal
types in native Javascript. These [and similar] types will be returned as a string
. The best way in this case is to use custom types.
Connection details
All Postgres options
const sql = postgres('postgres://username:password@host:port/database', {
host : '',
port : 5432,
path : '',
database : '',
username : '',
password : '',
ssl : false,
max : 10,
max_lifetime : null,
idle_timeout : 0,
connect_timeout : 30,
no_prepare : false,
types : [],
onnotice : fn,
onparameter : fn,
debug : fn,
transform : {
column : fn,
value : fn,
row : fn
},
connection : {
application_name : 'postgres.js',
...
},
target_session_attrs : null,
fetch_types : true,
})
Note that max_lifetime = 60 * (30 + Math.random() * 30)
by default. This resolves to an interval between 45 and 90 minutes to optimize for the benefits of prepared statements and working nicely with Linux's OOM killer.
SSL
Although vulnerable to MITM attacks, a common configuration for the ssl
option for some cloud providers is to set rejectUnauthorized
to false
(if NODE_ENV
is production
):
const sql =
process.env.NODE_ENV === 'production'
?
postgres({ ssl: { rejectUnauthorized: false } })
: postgres()
For more information regarding ssl
with postgres
, check out the Node.js documentation for tls.
Multi-host connections - High Availability (HA)
Multiple connection strings can be passed to postgres()
in the form of postgres('postgres://localhost:5432,localhost:5433', ...)
. This works the same as native the psql
command. Read more at multiple host uris
Connections will be attempted in order of the specified hosts/ports. On a successful connection, all retries will be reset. This ensures that hosts can come up and down seamlessly.
If you specify target_session_attrs: 'primary'
or PGTARGETSESSIONATTRS=primary
Postgres.js will only connect to the primary host, allowing for zero downtime failovers.
The Connection Pool
Connections are created lazily once a query is created. This means that simply doing const sql = postgres(...)
won't have any effect other than instantiating a new sql
instance.
No connection will be made until a query is made.
This means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using sql.begin()
, and automatically returned to the pool once your transaction is done.
Any query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic "onerror" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.
There are no guarantees about queries executing in order unless using a transaction with sql.begin()
or setting max: 1
. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.
Since this library automatically creates prepared statements, it also has a default max lifetime for connections to prevent memory bloat on the database itself. This is a random interval for each connection between 45 and 90 minutes. This allows multiple connections to come up and down seamlessly without user interference.
Connection timeout
By default, connections will not close until .end()
is called. However, it may be useful to have them close automatically when:
- re-instantiating multiple
sql``
instances - using Postgres.js in a Serverless environment (Lambda, etc.)
- using Postgres.js with a database service that automatically closes connections after some time (see
ECONNRESET
issue)
This can be done using the idle_timeout
or max_lifetime
options. These configuration options specify the number of seconds to wait before automatically closing an idle connection and the maximum time a connection can exist, respectively.
For example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:
const sql = postgres({
idle_timeout: 20,
max_lifetime: 60 * 30
})
Auto fetching of array types
Postgres.js will automatically fetch table/array-type information when it first connects to a database.
If you have revoked access to pg_catalog
this feature will no longer work and will need to be disabled.
You can disable this feature by setting fetch_types
to false
.
Environmental variables
It is also possible to connect to the database without a connection string or any options. Postgres.js will fall back to the common environment variables used by psql
as in the table below:
const sql = postgres()
Option | Environment Variables |
---|
host | PGHOST |
port | PGPORT |
database | PGDATABASE |
username | PGUSERNAME or PGUSER |
password | PGPASSWORD |
idle_timeout | PGIDLE_TIMEOUT |
connect_timeout | PGCONNECT_TIMEOUT |
Prepared statements
Prepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the no_prepare
option. For instance β this is useful when using PGBouncer in transaction mode
.
Custom Types
You can add ergonomic support for custom types, or simply use sql.typed(value, type)
inline, where type is the PostgreSQL oid
for the type and the correctly serialized string. (oid
values for types can be found in the pg_catalog.pg_types
table.)
Adding Query helpers is the cleanest approach which can be done like this:
const sql = postgres({
types: {
rect: {
to : 1337,
from : [1337],
serialize : ({ x, y, width, height }) => [x, y, width, height],
parse : ([x, y, width, height]) => { x, y, width, height }
}
}
})
const [custom] = sql`
insert into rectangles (
name,
rect
) values (
'wat',
${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }
)
returning *
`
Teardown / Cleanup
To ensure proper teardown and cleanup on server restarts use await sql.end()
before process.exit()
.
Calling sql.end()
will reject new queries and return a Promise which resolves when all queries are finished and the underlying connections are closed. If a { timeout }
option is provided any pending queries will be rejected once the timeout (in seconds) is reached and the connections will be destroyed.
Sample shutdown using Prexit
import prexit from 'prexit'
prexit(async () => {
await sql.end({ timeout: 5 })
await new Promise(r => server.close(r))
})
Error handling
Errors are all thrown to related queries and never globally. Errors coming from database itself are always in the native Postgres format, and the same goes for any Node.js errors eg. coming from the underlying connection.
Query errors will contain a stored error with the origin of the query to aid in tracing errors.
Query errors will also contain the query
string and the parameters
. These are not enumerable to avoid accidentally leaking confidential information in logs. To log these it is required to specifically access error.query
and error.parameters
, or set debug: true
in options.
There are also the following errors specifically for this library.
UNSAFE_TRANSACTION
Only use sql.begin or max: 1
To ensure statements in a transaction runs on the same connection (which is required for them to run inside the transaction), you must use sql.begin(...)
or only allow a single connection in options (max: 1
).
UNDEFINED_VALUE
Undefined values are not allowed
Postgres.js won't accept undefined
as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use null
explicitly.
MESSAGE_NOT_SUPPORTED
X (X) is not supported
Whenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.
MAX_PARAMETERS_EXCEEDED
Max number of parameters (65534) exceeded
The postgres protocol doesn't allow more than 65534 (16bit) parameters. If you run into this issue there are various workarounds such as using sql([...])
to escape values instead of passing them as parameters.
SASL_SIGNATURE_MISMATCH
Message type X not supported
When using SASL authentication the server responds with a signature at the end of the authentication flow which needs to match the one on the client. This is to avoid man-in-the-middle attacks. If you receive this error the connection was canceled because the server did not reply with the expected signature.
NOT_TAGGED_CALL
Query not called as a tagged template literal
Making queries has to be done using the sql function as a tagged template. This is to ensure parameters are serialized and passed to Postgres as query parameters with correct types and to avoid SQL injection.
AUTH_TYPE_NOT_IMPLEMENTED
Auth type X not implemented
Postgres supports many different authentication types. This one is not supported.
CONNECTION_CLOSED
write CONNECTION_CLOSED host:port
This error is thrown if the connection was closed without an error. This should not happen during normal operations, so please create an issue if this was unexpected.
CONNECTION_ENDED
write CONNECTION_ENDED host:port
This error is thrown if the user has called sql.end()
and performed a query afterward.
CONNECTION_DESTROYED
write CONNECTION_DESTROYED host:port
This error is thrown for any queries that were pending when the timeout to sql.end({ timeout: X })
was reached.
CONNECTION_CONNECT_TIMEOUT
write CONNECTION_CONNECT_TIMEOUT host:port
This error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using connect_timeout
or PGCONNECT_TIMEOUT
.
TypeScript support
postgres
has TypeScript support. You can pass a row list type for your queries in this way:
interface User {
id: number
name: string
}
const users = await sql<User[]>`SELECT * FROM users`
users[0].id
users[1].name
users[0].invalid
However, be sure to check the array length to avoid accessing properties of undefined
rows:
const users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`
if (!users.length)
throw new Error('Not found')
return users[0]
You can also prefer destructuring when you only care about a fixed number of rows.
In this case, we recommand you to prefer using tuples to handle undefined
properly:
const [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`
if (!user)
throw new Error('Not found')
return user
const [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`
const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}`
We do our best to type all the public API, however types are not always updated when features are added ou changed. Feel free to open an issue if you have trouble with types.
Migration tools
Postgres.js doesn't come with any migration solution since it's way out of scope, but here are some modules that support Postgres.js for migrations:
Thank you
A really big thank you to @JAForbes who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.
Thanks to @ACXgit for initial tests and dogfooding.
Also thanks to Ryan Dahl for letting me have the postgres
npm package name.