Use Neon Postgres with Railway
Connect a Neon Postgres database to your Node application deployed with Railway
Railway is an application deployment platform that allows users to develop web applications locally, provision infrastructure and then deploy to the cloud. Railway integrates with GitHub for continuous deployment and supports a variety of programming languages and frameworks.
This guide shows how to deploy a simple Node.js application connected to a Neon Postgres database on Railway.
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 Railway account. If you do not have one, sign up at Railway to get started.
- A GitHub account. Railway integrates with Gitub 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
plant_care_log
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 retrieves the list of plants tended to within the last month. 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 plant_care_log
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 plant_care_log
table.
Push Your application to GitHub
To deploy your application to Railway, 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 Railway
Creating a new Railway project
Log in to your Railway account and navigate to the dashboard. Click on the New Project
button and select the Deploy from Github repo
option. Pick the repository you created above, which sets off a Railway deployment.
Railway automatically figures out the type of application you're deploying and sets up the necessary build and start commands. However, we still need to add the DATABASE_URL
environment variable to connect to our Neon database.
Select the project and navigate to the Variables
tab. Add a new variable named DATABASE_URL
and set its value to your Neon database connection string. You can redeploy the project by clicking on Redeploy
from the context menu of the latest deployment.
Verify Deployment
Once the deployment completes and is marked as ACTIVE
, Railway 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, Railway will automatically build and deploy the changes to your web service.
Removing Your Application and Neon Project
To remove your application from Railway, select the project and navigate to the Settings
tab. Scroll down to the end to find the "Delete 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 detail, see Getting Support.