Use Neon Postgres with Render
Connect a Neon Postgres database to your Node application deployed with Render
Render is a comprehensive cloud service that provides hosting for web applications and static sites, with PR previews, zero-downtime deployments, and more. Render supports full-stack applications, offering both web services and background workers.
This guide shows how to deploy a simple Node.js application connected to a Neon Postgres database on Render.
Prerequisites
To follow along with this guide, you will need:
- A Neon account. 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. - A Render account. If you do not have one, sign up at Render to get started.
- A GitHub account. Render integrates with public GitHub providers for continuous deployment. So, you'd need a GitHub account to upload your application code.
- Node.js and npm installed on your local machine. We'll use Node.js to build and test the application locally.
Setting up your Neon database
Initialize a new project
Log in to the Neon Console and navigate to the Projects section.
-
Click the
New Project
button to create a new project. -
From your project dashboard, navigate to the
SQL Editor
from the sidebar, and run the following SQL command to create a new table in your database:Next, we insert some sample data into the
books_to_read
table, so we can query it later:
Retrieve your Neon database connection string
Log in to the Neon Console and navigate to the Connection Details section to find your database connection string. It should look similar to this:
Keep your connection string handy for later use.
Implementing the Node.js application
We'll create a simple Express application that connects to our Neon database and retrieve the sample data from the books_to_read
table. Run the following commands in a terminal to set it up.
We use the npm pkg set type="module"
command to enable ES6 module support in our project. We also create a new .env
file to store the DATABASE_URL
environment variable, which we'll use to connect to our Neon database. Lastly, we install the pg
library which is the Postgres driver we use to connect to our database.
Now, create a new file named index.js
and add the following code:
This code sets up an Express server that listens for requests on port 3000. When a request is made to the URL
, the server queries the books_to_read
table in your Neon database and returns the results as JSON.
We can test this application locally by running:
Now, navigate to http://localhost:3000/
in your browser to check that it returns the sample data from the books_to_read
table.
Push Your application to GitHub
To deploy your application to Render, you need to push your code to a GitHub repository. Create a new repository on GitHub by navigating to GitHub - New Repo. You can then push your code to the new repository using the following commands:
You can visit the GitHub repository to verify that your code has been pushed successfully.
Deploying to Render
Create a New Web Service on Render
Log in to your Render account and navigate to the dashboard. Click on the New +
button and select "Web Service". Pick the option to build and deploy
from a Git repository.
Next, choose the GitHub repository hosting the Node.js application we created above. Configure your web service as follows: - Environment: Select "Node". - Build Command: Enter npm install
. - Start Command: Enter node index.js
. - Environment Variables: Add your Neon database connection string from earlier as an environment variable: - Name: DATABASE_URL
- Value: {NEON_DATABASE_CONNECTION_STRING}
Click "Create Web Service" to finish. Render will automatically deploy your application and redirect you to the service dashboard, showing the deployment progress and the logs.
Verify Deployment
Once the deployment completes, Render provides a public URL for accessing the web service. Visit the provided URL to verify that your application is running and can connect to your Neon database.
Whenever you update your code and push it to your GitHub repository, Render will automatically build and deploy the changes to your web service.
Removing Your Application and Neon Project
To remove your application from Render, navigate to the dashboard, select Settings
for the deployed application, and scroll down to find the "Delete Web Service" option.
To delete your Neon project, follow the steps outlined in the Neon documentation under Delete a project.
Source code
You can find the source code for the application described in this guide on GitHub.
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 details, see Getting Support.