deploy/classic/neon-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 Neon Postgres database from an application deployed on Deno Deploy.
To get started, we need to create a new Postgres instance for us to connect to. For this tutorial, we will be using Neon Postgres as they provide free, managed Postgres instances. If you like to host your database somewhere else, you can do that too.
Visit https://neon.tech/ and click Sign up to sign up with an email, Github, Google, or partner account. After signing up, you are directed to the Neon Console to create your first project.
Enter a name for your project, select a Postgres version, provide a database name, and select a region. Generally, you'll want to select the region closest to your application. When you're finished, click Create project.
You are presented with the connection string for your new project, which you can use to connect to your database. Save the connection string, which looks something like this:
postgres://alex:[email protected]/dbname?sslmode=require
You will need the connection string in the next step.
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 set to the connection string you saved in
the last step.To read/write to Postgres using the
Neon serverless driver, first install it
using the deno add command:
deno add jsr:@neon/serverless
This will create or update your deno.json file with the dependency:
{
"imports": {
"@neon/serverless": "jsr:@neon/serverless@^0.10.1"
}
}
Now you can use the driver in your code:
import { neon } from "@neon/serverless";
// Get the connection string from the environment variable "DATABASE_URL"
const databaseUrl = Deno.env.get("DATABASE_URL")!;
// Create a SQL query executor
const sql = neon(databaseUrl);
try {
// Create the table
await sql`
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL
)
`;
} catch (error) {
console.error(error);
}
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.