These examples demonstrate how to connect a Python application to a PlanetScale database using three different methods:
- Django — Connect a Django application to PlanetScale
mysql-connector-python
driver — Connect a Python application to PlanetScale usingmysql-connector-python
.- MySQLdb interface — Connect a Python application to PlanetScale using the MySQLdb interface.
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 "Python" or "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.
To connect, find the method you're connecting with below and follow the instructions in that section.
- Install
mysql-connector-python
:
pip install mysql-connector-python
- Copy the contents of
mysql-connector-python.py
into your connection file. - Replace the placeholders for
HOSTNAME
,DATABASE
,USERNAME
, andPASSWORD
with the copied values from the previous section. We encourage you to move these placeholder values into your.env
file. - To ensure a secure connection, you must fill in the SSL certificate path. This is the value for
ssl_ca
in theconfig
object.
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.
Note: If you're developing on Windows, we recommend using mysql-connector-python
instead of mysqlclient
as it is easier to enable SSL.
- Install
mysqlclient
:
pip install mysqlclient
- Copy the contents of
mysql.py
into your connection file. - Replace the placeholders for
HOSTNAME
,DATABASE
,USERNAME
, andPASSWORD
with the copied values from the previous section. We encourage you to move these placeholder values into your.env
file. - To ensure a secure connection, you must fill in the SSL certificate path. This is the value for
ca
in thessl
object.
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.
- Copy the contents of
django/my.cnf
into your connection file. This may be a.env
file. - Replace the placeholders for
HOSTNAME
,DATABASE
,USERNAME
, andPASSWORD
with 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.py
file 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.