🇦🇺 Hello Australia! Sydney region is officially live on Neon.Sign Up
Guides/Languages/Python

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:

  1. Create a Neon project
  2. Configure Django connection settings

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:

  1. Navigate to the Projects page in the Neon Console.
  2. Click New Project.
  3. 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.

In your Django project, navigate to the DATABASES section of your settings.py file and modify the connection details as shown:

DATABASES = {    'default': {        'ENGINE': 'django.db.backends.postgresql',        'NAME': '[dbname]',        'USER': '[user]',        'PASSWORD': '[password]',        'HOST': '[hostname]',        'PORT': '[port]',         'OPTIONS': {             'sslmode': 'require',        }    }}

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.

note

Running Django tests is currently not supported. The Django test runner must be able to create a database for tests, which is not yet supported by Neon.

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 a Endpoint 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. The psycopg2 driver uses the libpq client library, which supports SNI as of v14. You can check your psycopg2 and libpq versions by starting a Django shell in your Django project and running the following commands:

# Start a Django shellpython3 manage.py shell# Check versionsimport psycopg2print("psycopg2 version:", psycopg2.__version__)print("libpq version:", psycopg2._psycopg.libpq_version())

The version number for libpq is presented in a different format, for example, version 14.1 will be shown as 140001. If your libpq version is less than version 14, you can either upgrade your psycopg2 driver to get a newer libpq version or use one of the workarounds described in our Connection errors documentation. Upgrading your psycopg2 driver may introduce compatibility issues with your Django or Python version, so you should test your application thoroughly.

Video course: Micro eCommerce with Django and Neon

Watch Justin Mitchel's video course, Micro eCommerce with Python, Django, Neon Serverless Postgres, Stripe, TailwindCSS and more, to learn how to connect a Django application to Neon.

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. Neon Pro Plan users can open a support ticket from the console. For more detail, see Getting Support.

Last updated on

Edit this page
Was this page helpful?