deploy/classic/postgres.md
:::info Legacy Documentation
You are viewing legacy documentation for Deno Deploy Classic. We recommend migrating to the new <a href="/deploy/">Deno Deploy</a> platform.
:::
This tutorial covers how to connect to a Postgres database from an application deployed on Deno Deploy.
This tutorial will focus entirely on connecting to Postgres unencrypted. If you would like to use encryption with a custom CA certificate, use the documentation here.
To get started, we need to create a new Postgres instance for us to connect to. For this tutorial, we will be using Supabase as they provide free, managed Postgres instances. If you like to host your database somewhere else, you can do that too.
Once you've set up your Postgres database, gather your connection information from your Postgres instance.
For the Supabase instance above, to get your connection information:
If you are using psql, you should generally be able to find your connection information by running:
test=# \conninfo
Your Postgres connection string will take the form:
postgres://user:[email protected]:5432/deploy?sslmode=disable
Next, let's create a project in Deno Deploy Classic and set it up with the requisite environment variables:
DATABASE_URL - The value should be your connection string that you retrieved
in the last step.To read/write to Postgres, import a suitable Postgres module such as this one from JSR, read the connection string from the environment variables, and create a connection pool.
import { Pool } from "jsr:@bartlomieju/postgres";
// Get the connection string from the environment variable "DATABASE_URL"
const databaseUrl = Deno.env.get("DATABASE_URL")!;
// Create a database pool with three connections that are lazily established
const pool = new Pool(databaseUrl, 3, true);
// Connect to the database
const connection = await pool.connect();
try {
// Create the table
await connection.queryObject`
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL
)
`;
} finally {
// Release the connection back into the pool
connection.release();
}
Once you have finished writing your application, you can deploy it on Deno Deploy Classic.
To do this, go back to your project page at
https://dash.deno.com/projects/<project-name>.
You should see a couple of options to deploy:
deployctl
deployctl deploy --project=<project-name> <application-file-name>
Unless you want to add a build step, we recommend that you select the Github integration.
For more details on the different ways to deploy on Deno Deploy Classic and the different configuration options, read here.