Create a Heroku Postgres Client in Node.js

For Salesforce Functions written in Node.js, use the node-postgres (pg) package to access your Heroku PostgreSQL database. Access the DATABASE_URL environment variable with process.env to create a Postgres client instance.

1import { Client } from "pg";
2async function pgConnect() {
3  const DATABASE_URL = process.env.DATABASE_URL;
4  if (!DATABASE_URL) {
5    throw new Error("DATABASE_URL is not set");
6  }
7  // Connect to PostgreSQL
8  const client = new Client({
9    connectionString: DATABASE_URL,
10    ssl: {
11      rejectUnauthorized: false,
12    },
13  });
14
15  await client.connect();
16  return client;
17}

Product Retirement Announcement

Salesforce Functions is no longer available for purchase or renewal. To preserve the capabilities that Salesforce Functions provided to your org, deploy an alternative solution before your existing order term ends. See Salesforce Functions Retirement for more information on migrating your functions. Contact your Salesforce Account Executive for more information on Heroku.