Use Neon with Deno Deploy
Connect a Neon Postgres database to your Deno Deploy application
Deno Deploy is a scalable serverless platform for running JavaScript, TypeScript, and WebAssembly at the edge, designed by the creators of Deno. It simplifies the deployment process and offers automatic scaling, zero-downtime deployments, and global distribution.
This guide demonstrates how to connect to a Neon Postgres database from a simple Deno application that uses deno-postgres driver to interact with the database.
The guide covers two deployment options:
- Deploying your application locally with Deno Runtime
- Deploying your application with the Deno Deploy serverless platform
Prerequisites
To follow the instructions in this guide, you will need:
- A Neon project. If you do not have one, sign up at Neon. Your Neon project comes with a ready-to-use Postgres database named
neondb
. We'll use this database in the following examples. - To use the Deno Deploy serverless platform, you require a Deno Deploy account. Visit Deno Deploy to sign up or log in.
Retrieve your Neon database connection string
Retrieve your database connection string from the Connection Details widget in the Neon Console.
Your connection string should look something like this:
You'll need the connection string a little later in the setup.
Deploy your application locally with Deno Runtime
Deno Runtime is an open-source runtime for TypeScript and JavaScript. The following instructions describe how to deploy an example application locally using Deno Runtime.
Install the Deno Runtime and deployctl
Follow the Install Deno and deployctl instructions in the Deno documentation to install the Deno runtime and deployctl
command-line utility on your local machine.
Create the example application
Next, create the server.ts
script on your local machine.
The script creates a table named books
in the neondb
database if it does not exist and inserts some data into it. It then starts a server that listens for requests on the /books
endpoint. When a request is received, the script returns data from the books
table.
Run the script locally
To run the script locally, set the DATABASE_URL
environment variable to the Neon connection string you copied earlier.
Then, run the command below to start the app server. The --allow-env
flag allows the script to access the environment variables, and the --allow-net
flag allows the script to make network requests. If the Deno runtime prompts you to allow these permissions, enter y
to continue.
Query the endpoint
You can request the /books
endpoint with a cURL
command to view the data returned by the script:
The cURL
command should return the following data:
Deploy your application with Deno Deploy
Deno Deploy is a globally distributed platform for serverless JavaScript applications. Your code runs on managed servers geographically close to your users, enabling low latency and faster response times. Deno Deploy applications run on light-weight V8 isolates powered by the Deno runtime.
Set up the project
-
If you have not done so already, install the
deployctl
command-line utility, as described above. -
If you have not done so already, create the example
server.ts
application on your local machine, as described above. -
Register or log in to Deno and navigate to the Create a project page, where you can select a project template for your preferred framework, link a code repo, or create an empty project.
-
The example application in this guide is a simple Deno script you've created locally, so let's select the Create an empty project option. Note the name of your Deno Deploy project. You will need it in a later step. Projects are given a generated Heroku-style name, which looks something like this:
cloudy-otter-57
. -
Click the
Settings
button and add aDATABASE_URL
environment variable. Set the value to your Neon connection string and click Save. -
To authenticate
deployctl
from the terminal, you will need an access token for your Deno Deploy account. Navigate back to your Deno dashboard and create a new access token. Copy the token value and set theDENO_DEPLOY_TOKEN
environment variable on your local machine by running this command from your terminal:
Deploy using deployctl
To deploy the application, navigate to the directory of your server.ts
application, and run the following command:
The --prod
flag specifies that the application should be deployed to the production environment.
The deployctl
command deploys the application to the Deno Deploy serverless platform. Once the deployment is complete, you'll see a message similar to the following:
Verifying the deployment
You can now access the application at the URL specified in the output. You can verify its connection to your Neon database by visiting the /books
endpoint in your browser or using cURL
to see if the data is returned as expected.
To check the health of the deployment or modify settings, navigate to the Project Overview page and select your project from the Projects list.
Deploying using Github
When deploying a more complex Deno application, with custom build steps, you can use Deno's Github integration. The integration lets you link a Deno Deploy project to a GitHub repository. For more information, see Deploying with GitHub.
Removing the example application and Neon project
To delete the example application on Deno Deploy, follow these steps:
- From the Deno Deploy dashboard, select your Project.
- Select the Settings tab.
- In the Danger Zone section, click Delete and follow the instructions.
To delete your Neon project, refer to Delete a project.
Resources
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.
Last updated on