Use Neon with Deno Deploy
Connect a Neon Postgres database to your Deno Deploy application
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
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
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
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:
cURL command should return the following data:
Deploy your application with Deno Deploy
Set up the project
If you have not done so already, install the
deployctlcommand-line utility, as described above.
If you have not done so already, create the example
server.tsapplication on your local machine, as described above.
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:
Settingsbutton and add a
DATABASE_URLenvironment variable. Set the value to your Neon connection string and click Save.
deployctlfrom 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 the
DENO_DEPLOY_TOKENenvironment 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:
--prod flag specifies that the application should be deployed to the production environment.
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.
Last updated on