
Company News
Socket Has Acquired Secure Annex
Socket has acquired Secure Annex to expand extension security across browsers, IDEs, and AI tools.
@vercel/postgres
Advanced tools
A client that works with Vercel Postgres.
Note: If you want to use an ORM instead of writing your own queries, see @vercel/postgres-kysely.
pnpm install @vercel/postgres
// Don't need any custom config?:
import { sql } from '@vercel/postgres';
// `sql` is already set up and ready to go; no further action needed
// Need to customize your config?:
import { createPool } from '@vercel/postgres';
const pool = createPool({
/* config */
});
// Need a single client?:
import { createClient } from '@vercel/postgres';
const client = createClient({
/* config */
});
// no-config
import { sql } from '@vercel/postgres';
const id = 100;
// A one-shot query
const { rows } = await sql`SELECT * FROM users WHERE id = ${userId};`;
// Multiple queries on the same connection (improves performance)
// warning: Do not share clients across requests and be sure to release them!
const client = await sql.connect();
const { rows } = await client.sql`SELECT * FROM users WHERE id = ${userId};`;
await client.sql`UPDATE users SET status = 'satisfied' WHERE id = ${userId};`;
client.release();
The sql import in the query above is just a modified Pool object (that's why you can call it). If you're running a custom config with createPool, the same functionality is available as pool.sql.
To specify a connection string:
import { createPool } from '@vercel/postgres';
const pool = createPool({
connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,
});
const likes = 100;
const { rows, fields } =
await pool.sql`SELECT * FROM posts WHERE likes > ${likes};`;
In edge environments, IO connections cannot be reused between requests. To allow your Pools to continue to function, we set maxUses to 1 when running on the edge (otherwise the Pool might hold on to a Client used in one request and try to use it again in another). Unfortunately, this means the Pool also can't reuse the connection within the request. For this reason, if you're firing more than one database query to serve a single request in your app, we recommend obtaining a Client from Pool.connect, using that Client to query the database, and then releasing it.
If you just want the connection URL, you can call postgresConnectionString(type: 'pool' | 'direct'): string;. This will read from your environment variables. For the pool type, it will look for the POSTGRES_URL environment variables. For the direct type, it will look for the POSTGRES_URL_NON_POOLING environment variables.
import { postgresConnectionString } from '@vercel/postgres';
const pooledConnectionString = postgresConnectionString('pool');
const directConnectionString = postgresConnectionString('direct');
When using the createClient or createPool functions, you can pass in additional options alongside the connection string that conforms to VercelPostgresClientConfig or VercelPostgresPoolConfig.
The @vercel/postgres package uses the pg package. For
more detailed documentation, checkout node-postgres.
@vercel/postgres reads database credentials from the environment variables on process.env. In general, process.env is automatically populated from your .env file during development, which is created when you run vc env pull. However, Vite does not expose the .env variables on process.env.
You can fix this in one of following two ways:
process.env yourself using something like dotenv-expand:pnpm install --save-dev dotenv dotenv-expand
// vite.config.js
import dotenvExpand from 'dotenv-expand';
import { loadEnv, defineConfig } from 'vite';
export default defineConfig(({ mode }) => {
// This check is important!
if (mode === 'development') {
const env = loadEnv(mode, process.cwd(), '');
dotenvExpand.expand({ parsed: env });
}
return {
...
};
});
$env/static/private:import { createPool } from '@vercel/postgres';
+ import { POSTGRES_URL } from '$env/static/private';
import { createPool } from '@vercel/postgres';
const pool = createPool({
- /* config */
+ connectionString: POSTGRES_URL
});
The 'pg' package is a popular PostgreSQL client for Node.js. It offers a comprehensive set of features for interacting with PostgreSQL databases, including connection pooling, query building, and transaction management. Compared to @vercel/postgres, 'pg' is more feature-rich and widely used in various environments, not just serverless.
Knex.js is a SQL query builder for Node.js that supports multiple database types, including PostgreSQL. It provides a powerful and flexible API for building and executing SQL queries. While @vercel/postgres is focused on simplicity and integration with Vercel, Knex.js offers more advanced query building capabilities and is suitable for complex database interactions.
Sequelize is a promise-based Node.js ORM for various SQL databases, including PostgreSQL. It provides a higher-level abstraction for database operations, allowing developers to work with models and associations. Compared to @vercel/postgres, Sequelize offers more advanced ORM features, making it suitable for applications that require complex data relationships and model management.
FAQs
Connect to Vercel Postgres databases on the Edge
The npm package @vercel/postgres receives a total of 245,196 weekly downloads. As such, @vercel/postgres popularity was classified as popular.
We found that @vercel/postgres demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket has acquired Secure Annex to expand extension security across browsers, IDEs, and AI tools.

Research
/Security News
Socket is tracking cloned Open VSX extensions tied to GlassWorm, with several updated from benign-looking sleepers into malware delivery vehicles.

Product
Reachability analysis for PHP is now available in experimental, helping teams identify which vulnerabilities are actually exploitable.