Skip to content

Commit

Permalink
docs: update neon postgres guide to use neon serverless driver on jsr (
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanushreddy291 authored Oct 25, 2024
1 parent ac0059e commit 94e2df9
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions deploy/manual/neon-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ top of Postgres [here](../tutorials/tutorial-postgres).

## Setup Postgres

> 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](https://deno-postgres.com/#/?id=ssltls-connection).
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](https://neon.tech/) as they
provide free, managed Postgres instances. If you like to host your database
Expand Down Expand Up @@ -53,32 +49,45 @@ environment variables:

## Write code that connects to Postgres

To read/write to Postgres, import the Postgres module, read the connection
string from the environment variables, and create a connection pool.
To read/write to Postgres using the
[Neon serverless driver](https://deno.com/blog/neon-on-jsr), first install it
using the `deno add` command:

```sh
deno add jsr:@neon/serverless
```

This will create or update your `deno.json` file with the dependency:

```json
{
"imports": {
"@neon/serverless": "jsr:@neon/serverless@^0.10.1"
}
}
```

Now you can use the driver in your code:

```ts
import { Pool } from "https://deno.land/x/[email protected]/mod.ts";
import { neon } from "@neon/serverless";

// 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();
// Create a SQL query executor
const sql = neon(databaseUrl);

try {
// Create the table
await connection.queryObject`
await sql`
CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL
)
`;
} finally {
// Release the connection back into the pool
connection.release();
} catch (error) {
console.error(error);
}
```

Expand Down

0 comments on commit 94e2df9

Please sign in to comment.