Connect a Django application to Neon
Set up a Neon project in seconds and connect from a Django application
To connect to Neon from a Django 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.
To create a Neon project:
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify your project settings and click Create Project.
Configure Django connection settings
Connecting to Neon requires configuring database connection settings in your Django project's settings.py
file.
note
To avoid the endpoint ID is not specified
connection issue described here, be sure that you are using an up-to-date driver.
In your Django project, navigate to the DATABASES
section of your settings.py
file and modify the connection details as shown:
note
Neon places computes into an idle state and closes connections after 5 minutes of inactivity (see Compute lifecycle). To avoid connection errors, you can set the Django CONN_MAX_AGE setting to 0 to close database connections at the end of each request so that your application does not attempt to reuse connections that were closed by Neon. From Django 4.1, you can use a higher CONN_MAX_AGE
setting in combination with the CONN_HEALTH_CHECKS setting to enable connection reuse while preventing errors that might occur due to closed connections. For more information about these configuration options, see Connection management, in the Django documentation.
You can find all of the connection details listed above in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application.
For additional information about Django project settings, see Django Settings: Databases, in the Django documentation.
Connection issues
-
Django uses the
psycopg2
driver as the default adapter for Postgres. If you have an older version of that driver, you may encounter anEndpoint ID is not specified
error when connecting to Neon. This error occurs if the client library used by your driver does not support the Server Name Indication (SNI) mechanism in TLS, which Neon uses to route incoming connections. Thepsycopg2
driver uses thelibpq
client library, which supports SNI as of v14. You can check yourpsycopg2
andlibpq
versions by starting a Django shell in your Django project and running the following commands:The version number for
libpq
is presented in a different format, for example, version 14.1 will be shown as 140001. If yourlibpq
version is less than version 14, you can either upgrade yourpsycopg2
driver to get a newerlibpq
version or use one of the workarounds described in our Connection errors documentation. Upgrading yourpsycopg2
driver may introduce compatibility issues with your Django or Python version, so you should test your application thoroughly. -
If you encounter an
SSL SYSCALL error: EOF detected
when connecting to the database, this typically occurs because the application is trying to reuse a connection after the Neon compute has been suspended due to inactivity. To resolve this issue, try one of the following options:- Set your Django
CONN_MAX_AGE
setting to a value less than or equal to the scale to zero setting configured for your compute. - Enable
CONN_HEALTH_CHECKS
by setting it totrue
. This forces a health check to verify that the connection is alive before executing a query.
For information configuring Neon's Scale to zero setting, see Configuring Scale to zero for Neon computes.
- Set your Django
Schema migration with Django
For schema migration with Django, see our guide:
Django application blog post and sample application
Learn how to use Django with Neon Postgres with this blog post and the accompanying sample application.
Blog Post: Using Django with Neon
Learn how to build a Django application with Neon Postgres
Django sample application
Django with Neon Postgres
Community 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.