Connect from Phoenix to Neon
Set up a Neon project in seconds and connect from Phoenix
This guide describes how to connect Neon in a Phoenix application. Ecto provides an API and abstractions for interacting databases, enabling Elixir developers to query any database using similar constructs.
It is assumed that you have a working installation of Elixir.
To connect to Neon from Phoenix with Ecto:
- Create a Neon project
- Store your Neon credentials
- Create a Phoenix project
- Build and Run the Phoenix application
Create a Neon project
If you do not have one already, create a Neon project. Save your connection details including your password. They are required when defining connection settings.
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify your project settings and click Create Project.
Store your Neon credentials
Add a .env
file to your project directory and add your Neon connection string to it. You can find the connection string for your database in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application.
You will need the connection string details later in the setup.
Create a Phoenix project
-
Create a Phoenix project if you do not have one, with the following command:
When prompted to, choose to not install the dependencies.
-
Update
config/dev.exs
file's configuration with your Neon database connection details. Use the connection details from the Neon connection string you copied previously.note
The
:ssl
option is required to connect to Neon. Postgrex, since v0.18, verifies the server SSL certificate and you need to select CA trust store using:cacerts
or:cacertfile
options. You can use the OS-provided CA store by settingcacerts: :public_key.cacerts_get()
. While not recommended, you can disable certificate verification by settingssl: [verify: :verify_none]
. -
Update
config/runtime.exs
file's configuration with your Neon database connection details. Use the connection details from the Neon connection string you copied previously. -
Update
config/test.exs
file's configuration with your Neon database connection details. Use the connection details from the Neon connection string you copied in the first part of the guide. -
Now, install the dependencies used in your Phoenix application using the following command:
-
Seed the Neon database with the following command:
Once that's done, move on to building and running the application in production mode.
Build and Run the Phoenix application
To compile the app in production mode, run the following command:
To compile assets for the production mode, run the following command:
For each deployment, a secret key is required for encrypting and signing data. Run the following command to generate the key:
When you run the following command, you can expect to see the Phoenix application on localhost:4001:
Source code
You can find the source code for the application described in this guide on GitHub.
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.