Use Neon with Netlify Functions
Connect a Neon Postgres database to your Netlify Functions application
Netlify Functions provide a serverless execution environment for building and deploying backend functionality without managing server infrastructure. It's integrated with Netlify's ecosystem, making it ideal for augmenting web applications with server-side logic, API integrations, and data processing tasks in a scalable way.
This guide will show you how to connect to a Neon Postgres database from your Netlify Functions project. We'll use the Neon serverless driver to connect to the database and make queries.
Prerequisites
Before starting, ensure you have:
- 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 Netlify account for deploying your site with
Functions
. Sign up at Netlify if necessary. While Netlify can deploy directly from a GitHub repository, we'll use theNetlify
CLI tool to deploy our project manually. - Node.js and npm installed locally for developing and deploying your Functions.
Setting up your Neon database
Initialize a new project
After logging into the Neon Console, proceed to the Projects section.
-
Click
New Project
to start a new one. -
In the Neon Dashboard, use the
SQL Editor
from the sidebar to execute the SQL command below, creating a new table for coffee blends:Populate the table with some initial data:
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.
Setting up your Netlify Functions project
We'll use the Netlify CLI to create a new project and add functions to it. To install the CLI, run:
To authenticate the CLI with your Netlify account, run:
This command opens a browser window to authenticate your terminal session with Netlify. After logging in, you can close the browser window and interact with your Netlify account from the terminal.
Create a new Netlify project
We will create a simple HTML webpage that fetches the coffee blends from the Neon database using a Netlify Function and displays them. To create a new Netlify Site
project, run:
You will be prompted to select a team and site name. Choose a unique name for your site. This command then links the current directory to a Site
project in your Netlify account.
Implement the function
We'll create a new function to fetch the coffee blends from the Neon database. To set up the function entrypoint script, you can run the command below and use the settings provided:
This command creates a new directory netlify/functions/get_coffee_blends
with a get_coffee_blends.js
file inside it. We are using the ES6 import
syntax to implement the request handler, so we will change the script extension to .mjs
for the runtime to recognize it.
We also install the Neon serverless
driver as a dependency to connect to the Neon database and fetch the data.
Now, replace the contents of the function script with the following code:
This function connects to your Neon database and fetches the list of your favorite coffee blends.
Implement the frontend
To make use of the Function
implemented above, we will create a simple HTML page that fetches and displays the coffee information by calling the function.
Create a new file index.html
at the root of your project with the following content:
Test the site locally
Set the DATABASE_URL
environment variable in a .env
file at the root of your project:
We are now ready to test our Netlify site project locally. Run the following command to start a local development server:
The Netlify CLI will print the local server URL where your site is running. Open the URL in your browser to see the coffee blends fetched from your Neon database.
Deploying your Netlify Site and Function
Deploying is straightforward with the Netlify CLI. However, we need to set the DATABASE_URL
environment variable for the Netlify deployed site too. You can use the CLI to set it.
Now, to deploy your site and function, run the following command. When asked to provide a publish directory, enter .
to deploy the entire project.
The CLI will build and deploy your site and functions to Netlify. After deployment, Netlify provides a URL for your live function. Navigate to the URL in your browser to check that the deployment was successful.
Removing the example application and Neon project
For cleanup, delete your Netlify site and functions via the Netlify dashboard or CLI. Consult the Netlify documentation for detailed instructions.
To remove your Neon project, follow the deletion steps in Neon's documentation under Manage Projects.
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.