Schema migration with Neon Postgres and Django
Set up Neon Postgres and run migrations for your Django project
Django is a high-level Python framework to make database-driven web applications. It provides an ORM (Object-Relational Mapping) layer that abstracts database operations, making it easy to interact with databases using Python code. Django also includes a powerful migration system that allows you to define and manage database schema changes over time.
This guide demonstrates how to use Django with a Neon Postgres database. We'll create a simple Django application and walk through the process of setting up the database, defining models, and generating and running migrations to manage schema changes.
Prerequisites
To follow along with this guide, you will need:
- A Neon account. If you do not have one, sign up at Neon. Your Neon project comes with a ready-to-use Postgres database named
neondb
. We'll use this database in the following examples. - Python installed on your local machine. We recommend using a newer version of Python, 3.8 or higher.
Setting up your Neon database
Initialize a new project
- Log in to the Neon Console and navigate to the Projects section.
- Select a project or click the
New Project
button to create a new one.
Retrieve your Neon database connection string
On your Neon project dashboard, navigate to the Connection Details section to find your database connection string. It should look similar to this:
Keep your connection string handy for later use.
Setting up the Django project
Set up the Python environment
To manage our Django project dependencies, we create a new Python virtual environment. Run the following commands in your terminal to set it up.
Activate the virtual environment by running the following command:
With the virtual environment activated, we can create a new directory for our Django project and install the required packages:
We installed Django and the psycopg2-binary
package to connect to the Neon Postgres database. We also added the python-dotenv
to read environment variables easily, and the dj-database-url
package to parse the Neon connection string into Django settings. We also saved the installed packages to a requirements.txt
file so the project can be easily recreated in another environment.
Create a new Django project
Run the following command to create a new Django project in the current directory:
This command creates a new Django project named guide_neon_django
in the current directory.
Set up the Database configuration
Create a .env
file in the project root directory and add the DATABASE_URL
environment variable to it. Use the connection string that you obtained from the Neon Console earlier.
For Django to read the environment variables from the .env
file, open the settings.py
file located in the guide_neon_django
directory and add the following code, updating the DATABASES
setting:
Create a new Django app
Inside your project directory, run the following command to create a new Django app:
This command creates a new app named catalog
inside the Django project.
Defining data models and running migrations
Specify the data model
Now, open the models.py
file in your catalog
app directory and define the database models for your application:
This code defines two models: Author
and Book
. The Author
model represents an author with fields for name
, bio
, and a created_at
timestamp. The Book
model represents a book with fields for title
, author
(as a foreign key to the Author
model), and a created_at
timestamp. Django automatically creates an id
field for each model as the primary key.
Generate migration files
We first add the new application catalog
to the list of installed apps for the Django project. Open the settings.py
file in the guide_neon_django
directory and add the catalog
app to the INSTALLED_APPS
setting:
To generate migration files based on the defined models, run the following command:
This command detects the new Author
and Book
models that were added and generates migration files in the catalog/migrations
directory.
Apply the migration
Now, to apply the migration and create the corresponding tables in the Neon Postgres database, run the following command:
This command executes the migration files and creates the necessary tables in the database. Note that Django creates multiple other tables, such as django_migrations
and auth_user
for its internal usage.
Seed the database
To populate the database with some initial data, we can create a custom management command for our app. Create a new file named populate.py
in the catalog/management/commands
directory.
Now, add the following code to the populate.py
file to create some authors and books:
Now, run the custom management command in your terminal and seed the database:
Implement the application
Create views to display data
We can now create views to display the authors and books in our application. Create a file views.py
in the catalog
app directory and add the following code:
We defined two views: list_authors
to list all authors and list_books_by_author
to list books by a specific author. The views return JSON responses with the serialized data.
Define URLs for the views
Next, create a file urls.py
in the catalog
app directory and add the following code:
The URLs are mapped to the views defined previously using the Django URL dispatcher.
Include the app URLs in the project
Finally, include the catalog
app URLs in the project's main urls.py
file, by updating the urlpatterns list:
Run the Django development server
To start the Django development server and test the application, run the following command:
Navigate to the url http://localhost:8000/catalog/authors/
in your browser to view the list of authors. You can also view the books by a specific author by visiting http://localhost:8000/catalog/books/<author_id>/
.
Applying schema changes
We will demonstrate how to handle schema changes by adding a new field country
to the Author
model, to store the author's country of origin.
Update the data model
Open the models.py
file in your catalog
app directory and add a new field to the Author
model:
Generate and run the migration
To generate a new migration file for the schema change, run the following command:
This command detects the updated Author
models and generates a new migration file to add the new field to the corresponding table in the database. Now, to apply the migration, run the following command:
Test the schema change
Restart the Django development server.
Navigate to the url http://localhost:8000/catalog/authors
to view the list of authors. You should see the new country
field included and set to empty for each author entry, reflecting the schema change.
Conclusion
In this guide, we demonstrated how to set up a Django project with Neon Postgres, define database models, and generate migrations and run them. Django's ORM and migration system make it easy to interact with the database and manage schema evolution over time.
Source code
You can find the source code for the application described in this guide on GitHub.
Resources
For more information on the tools and concepts used in this guide, refer to the following 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 detail, see Getting Support.
Last updated on