Deploy Your Node.js App with Neon Postgres on Heroku
A step-by-step guide to deploying a Node application with a Neon Postgres database on Heroku
Heroku is a popular platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It simplifies the deployment process, making it a favorite among developers for its ease of use and integration capabilities.
This guide walks you through deploying a simple Node.js application connected to a Neon Postgres database, on Heroku.
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 Heroku account. Sign up at Heroku to get started.
- Git installed on your local machine. Heroku uses Git for version control and deployment.
- 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 New Project to create a new project.
-
In your project dashboard, go to the SQL Editor and run the following SQL command to create a new table:
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 retrieves the list of music albums. Run the following commands in your terminal to set it up:
We use the npm pkg set type="module"
command to enable ES6 module support in our project. We'll 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.
In the .env
file, store your Neon database connection string:
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 music_albums
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 it returns the sample data from the music_albums
table.
Deploying to Heroku
Create a New Heroku App
We will use the Heroku CLI
to deploy our application to Heroku manually. You can install it on your machine by following the instructions here. Once installed, log in to your Heroku account using:
You will be prompted to log in to your Heroku account in the browser. After logging in, you can close the browser and return to your terminal.
Before creating the Heroku application, we need to initialize a new Git repository in our project folder:
Next, we can create a new app on Heroku using the following command. This creates a new Heroku app with the name neon-heroku-example
, and sets up a new Git remote for the app called heroku
.
You'll also need to set the DATABASE_URL
on Heroku to your Neon database connection string:
Deploy Your Application
To deploy your application to Heroku, use the following command to push your code to the heroku
remote. Heroku will automatically detect that your application is a Node.js application, install the necessary dependencies and deploy it.
Once the deployment is complete, you should see a message with the URL of your deployed application. Navigate to this URL in your browser to see your application live on Heroku.
You've now successfully deployed a Node.js application on Heroku that connects to a Neon Postgres database. For further customization and scaling options, you can explore the Heroku and Neon documentation.
Removing Your Application and Neon Project
To remove your application from Heroku, select the app from your Heroku dashboard. Navigate to the Settings
tab and scroll down to the end to find the "Delete App" 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 detail, see Getting Support.