Connect from Phoenix to Neon
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 const...
Pre-built prompt for connecting Phoenix applications to Lakebase Postgres
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.
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
.envfile to your project directory and add your Neon connection string to it. You can find your connection string by clicking the Connect button in the Console nav to open the Connect to your branch modal. For more information, see Connect from any application.Bash DATABASE_URL="postgresql://<user>:<password>@<endpoint_hostname>.neon.tech:<port>/<dbname>?sslmode=require&channel_binding=require"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:
Bash # install phx.new if you haven't already # mix archive.install hex phx_new mix phx.new helloWhen prompted, choose to not install the dependencies yet.
Configure database connections
Update the following configuration files with your database connection details from the connection string you copied earlier.
- Update
config/dev.exs:elixir config :hello, Hello.Repo, username: "neondb_owner", password: "JngqXejzvb93", hostname: "ep-rough-snowflake-a5j76tr5.us-east-2.aws.neon.tech", database: "neondb", stacktrace: true, show_sensitive_data_on_connection_error: true, pool_size: 10, ssl: [cacerts: :public_key.cacerts_get()]note
The
:ssloption is required to connect to the database. Postgrex, since v0.18, verifies the server SSL certificate and you need to select CA trust store using:cacertsor:cacertfileoptions. 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:elixir config :hello, Hello.Repo, ssl: [cacerts: :public_key.cacerts_get()], url: database_url, pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), socket_options: maybe_ipv6 - Update
config/test.exs:elixir config :hello, Hello.Repo, username: "neondb_owner", password: "JngqXejzvb93", hostname: "ep-rough-snowflake-a5j76tr5.us-east-2.aws.neon.tech", database: "with_phoenix_test#{System.get_env("MIX_TEST_PARTITION")}", pool: Ecto.Adapters.SQL.Sandbox, pool_size: System.schedulers_online() * 2, ssl: [cacerts: :public_key.cacerts_get()]tip
This guide configures the test environment for completeness but doesn't cover running tests. For production workflows, consider using Neon branches instead of a separate test database. Branches provide isolated, cost-effective copies of your database that are ideal for testing and CI/CD pipelines.
- Update
Install dependencies and create databases
- Install the dependencies:
Bash mix deps.get - Create the development and test databases:
Bash mix ecto.create MIX_ENV=test mix ecto.createThe first command creates the development database (
neondb). The second creates the test database (with_phoenix_test).
- Install the dependencies:
Build and run the Phoenix application
To compile the app in production mode, run the following command:
Bash MIX_ENV=prod mix compileTo compile assets for the production mode, run the following command:
Bash MIX_ENV=prod mix assets.deployFor each deployment, a secret key is required for encrypting and signing data. Run the following command to generate the key:
Bash mix phx.gen.secretWhen you run the following command, you can expect to see the Phoenix application at http://localhost:4001:
Bash PORT=4001 \ MIX_ENV=prod \ PHX_HOST=localhost \ DATABASE_URL="postgresql://...:...@...aws.neon.tech/neondb?sslmode=require&channel_binding=require" \ SECRET_KEY_BASE=".../..." \ mix phx.server
Next steps
Section titled “Next steps”- Add Object Storage: S3-compatible file storage that branches with your database
- Call an LLM with AI Gateway: Access foundation models from Anthropic, OpenAI, Google, and more with one credential
Need help?
Section titled “Need help?”Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.