---
title: "Using Python & Django with Neon’s Serverless Postgres"
description: "Step-by-step setup of a Django app backed by Neon's serverless Postgres: pyenv and venv for the Python environment, psycopg and python-dotenv for the connection with sslmode=require, then models, migrations, views and templates, with Gunicorn for production."
url: "https://swe.quest/articles/using-python-django-with-neon-s-serverless-postgres-f86e0ca09c40f824"
canonical: "https://neon.com/blog/python-django-and-neons-serverless-postgres"
source: "Neon"
published: "2024-02-01"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["python","postgres","serverless","performance"]
---

# Using Python & Django with Neon’s Serverless Postgres

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-02-01, 8 min read, How-to. Original: [https://neon.com/blog/python-django-and-neons-serverless-postgres](https://neon.com/blog/python-django-and-neons-serverless-postgres)

## Summary

Step-by-step setup of a Django app backed by Neon's serverless Postgres: pyenv and venv for the Python environment, psycopg and python-dotenv for the connection with sslmode=require, then models, migrations, views and templates, with Gunicorn for production. An Apache Bench test with 9 Gunicorn workers reached only 50 requests per second at 340ms average latency. Setting Django's CONN\_MAX\_AGE to enable persistent database connections raised throughput 8x, to 407 requests per second at 41ms average and 57ms p99.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Python](https://swe.quest/tags/python.md) (Languages)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- Python basics ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- Django basics

## Keywords

Django, Neon, Gunicorn, psycopg, CONN\_MAX\_AGE, pyenv, Apache Bench

## Related posts

- [Using Neon’s Scale to Zero with Long-Running Applications](https://swe.quest/articles/using-neon-s-scale-to-zero-with-long-running-applications-bbe64dd8466d86d8.md): Neon · 2024-01-24 · Medium · 7 min read · PostgreSQL, Serverless, Resilience, JavaScript, Performance engineering
  Neon's scale to zero suspends a Postgres compute after an idle window (only when no WAL senders are active and autovacuum isn't running), which severs connections held by long-running servers.
- [Quicker serverless Postgres connections](https://swe.quest/articles/quicker-serverless-postgres-connections-7a37a1071edd4362.md): Neon · 2023-03-28 · Medium · 8 min read · PostgreSQL, Networking, Serverless, Performance engineering, Cryptography
  Neon cut the network round-trips needed to get a first query result from its WebSocket-based serverless Postgres driver from nine to four.
- [Edge-compatible Serverless Driver for Postgres](https://swe.quest/articles/edge-compatible-serverless-driver-for-postgres-9b0d09c6c7a067d0.md): Neon · 2022-12-08 · Medium · 9 min read · PostgreSQL, Serverless, Networking, Cryptography, WebAssembly
  Neon's @neondatabase/serverless driver lets V8-isolate platforms like Cloudflare Workers, which lack TCP, speak the real Postgres wire protocol by tunnelling it over a WebSocket to an open-source Go proxy that relays to TCP.
- [Simplify Serverless in AWS: How to Use Neon and Ephemeral Environments](https://swe.quest/articles/simplify-serverless-in-aws-how-to-use-neon-and-ephemeral-environments-81cd9c7a09760341.md): Neon · 2025-05-05 · Medium · 10 min read · PostgreSQL, Serverless, CI/CD, Testing, JavaScript
  Neon's serverless Postgres pairs well with ephemeral environments on AWS Lambda because each environment can get its own instant copy-on-write database branch without paying for extra RDS uptime.
- [Performance tips for Neon Postgres](https://swe.quest/articles/performance-tips-for-neon-postgres-9739985c47432424.md): Neon · 2024-04-04 · Medium · 8 min read · PostgreSQL, Performance engineering, Serverless, SQL
  Neon's serverless Postgres follows mostly standard Postgres tuning, plus a few Neon-specific checks.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
