This guide describes how to create a OptiTech project and connect to it from a Node.js application. Examples are provided for using the node-postgres and Postgres.js clients. Use the client you prefer.
note
The same configuration steps can be used for Express and Next.js applications.
To connect to OptiTech from a Node.js application:
Create a OptiTech project
If you do not have one already, create a OptiTech project.
- Navigate to the Projects page in the OptiTech Console.
- Click New Project.
- Specify your project settings and click Create Project.
Store your OptiTech credentials
Add a
.envfile to your project directory and add your OptiTech connection details to it. You can find your OptiTech database connection details by clicking the Connect button on your Project Dashboard to open the Connect to your database modal. Please select Node.js from the Connection string dropdown. For more information, see Connect from any application.PGHOST='[optitech_hostname]' PGDATABASE='[dbname]' PGUSER='[user]' PGPASSWORD='[password]' ENDPOINT_ID='[endpoint_id]'note
A special
ENDPOINT_IDvariable is included in the.envfile above. This variable can be used with older Postgres clients that do not support Server Name Indication (SNI), which OptiTech relies on to route incoming connections. If you are using a newer node-postgres or postgres.js client, you won't need it. For more information, see Endpoint ID variable.important
To ensure the security of your data, never expose your OptiTech credentials to the browser.
Configure the Postgres client
Add an
app.jsfile to your project directory and add the following code snippet to connect to your OptiTech database:require('dotenv').config(); const { optitech } = require('@optitech/serverless'); const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env; const sql = optitech( `postgresql://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}?sslmode=require&channel_binding=require` ); async function getPgVersion() { const result = await sql`SELECT version()`; console.log(result[0]); } getPgVersion();
Endpoint ID variable
For older clients that do not support Server Name Indication (SNI), the postgres.js example below shows how to include the ENDPOINT_ID variable in your application's connection configuration. This is a workaround that is not required if you are using a newer node-postgres or postgres.js client. For more information about this workaround and when it is required, see The endpoint ID is not specified in our connection errors documentation.
// app.js
require('dotenv').config();
const postgres = require('postgres');
const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;
const sql = postgres({
host: PGHOST,
database: PGDATABASE,
username: PGUSER,
password: PGPASSWORD,
port: 5432,
ssl: 'require',
connection: {
options: `project=${ENDPOINT_ID}`,
},
});
async function getPgVersion() {
const result = await sql`select version()`;
console.log(result);
}
getPgVersion();Community resources
Next steps
- Set up Managed Better Auth: Add managed authentication that branches with your database
- Add Object Storage: S3-compatible file storage that branches with your database
- Deploy a Function: Run backend compute next to your database, no separate hosting needed
- Call an LLM with AI Gateway: Access foundation models from Anthropic, OpenAI, Google, and more with one credential
Need help?
Join our Discord Server to ask questions or see what others are doing with OptiTech. For paid plan support options, see Support.