This example demonstrates how to connect a Django application to a PlanetScale database.
Follow the instructions below to find and insert your PlanetScale credentials.
- In the PlanetScale dashboard, select the database you want to connect to.
- Click "Branches" and select the branch you want to connect to.
- Click "Connect".
- Select "Django" from the "Connect from" dropdown.
- If the password is blurred, click "New password".
- Copy the credentials. You won't be able to see the password again.
- Copy the contents of
django/my.cnfinto your connection file. This may be a.envfile. - Replace the placeholders for
HOSTNAME,DATABASE,USERNAME, andPASSWORDwith the copied values from the previous section.. - To ensure a secure connection, you must fill in the SSL certificate path. This is the value for
ssl_ca.
This path depends on your system, so you need to paste in the appropriate value.
You can find configuration information for your system in our Secure connections documentation.
- You can update your
settings.pyfile to read from your connection file, as shown in our example. - (Optional) If you're using Django's default migrations, you may run into issues while migrating, as PlanetScale doesn't support foreign key constraints. We've created a PlanetScale Django database wrapper that you can pull into your project to disable foreign key constraints.
For further implementation information, refer to the custom database wrapper section of our Django quickstart.
Django resources
- For more in-depth Django instructions, check out the Django quickstart or the sample Django application.
Next steps
- Learn more about the PlanetScale workflow.
- Learn how to branch your PlanetScale database for development.
- Learn how to ship schema changes to production with deploy requests.