@vercel/postgres 🚧
A client that works with Vercel Postgres.
Quick Start
Install
npm install @vercel/postgres
Basic Usage
Automatically Read Environment Variables
Default Export
import postgres from '@vercel/postgres';
await postgres.query('SELECT * from POSTS WHERE likes > 100;');
Named Export
import { postgres } from '@vercel/postgres';
await postgres.query('SELECT * from POSTS WHERE likes > 100;');
Set Connection String Manually (Client or Pool)
import { createClient, createPool } from '@vercel/postgres';
const client = createClient({
connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,
});
await client.query('SELECT * from POSTS WHERE likes > 100;');
const pool = createPool({
connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,
});
await pool.query('SELECT * from POSTS WHERE likes > 100;');
Parameterized Query
import { postgres } from '@vercel/postgres';
await postgres.query('SELECT * from POSTS WHERE author=$1;', ['rauchg']);
Connection Config
When using the createClient
or createPool
functions, you can pass in additional options to configuration object alongside the connection string that conforms to PostgresConfig
or PostgresPoolConfig
, respectively:
export interface PostgresConfig {
connectionString: string;
keepAlive?: boolean | undefined;
statement_timeout?: false | number | undefined;
query_timeout?: number | undefined;
keepAliveInitialDelayMillis?: number | undefined;
idle_in_transaction_session_timeout?: number | undefined;
application_name?: string | undefined;
connectionTimeoutMillis?: number | undefined;
max?: number | undefined;
min?: number | undefined;
idleTimeoutMillis?: number | undefined;
log?: ((...messages: unknown[]) => void) | undefined;
allowExitOnIdle?: boolean | undefined;
maxUses?: number | undefined;
}
export interface PostgresPoolConfig extends PostgresConfig {
max?: number | undefined;
min?: number | undefined;
idleTimeoutMillis?: number | undefined;
log?: ((...messages: unknown[]) => void) | undefined;
allowExitOnIdle?: boolean | undefined;
maxUses?: number | undefined;
}
Documentation
The @vercel/postgres
package uses the pg
under the hood. For
more detailed documentation, checkout node-postgres.