Skip to content

Latest commit

 

History

History

python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Connecting to PlanetScale from Python

These examples demonstrate how to connect a Python application to a PlanetScale database using three different methods:

Follow the instructions below to find and insert your PlanetScale credentials.

Getting the credentials

  1. In the PlanetScale dashboard, select the database you want to connect to.
  2. Click "Branches" and select the branch you want to connect to.
  3. Click "Connect".
  4. Select "Python" or "Django" from the "Connect from" dropdown.
  5. If the password is blurred, click "New password".
  6. Copy the credentials. You won't be able to see the password again.

Connecting your database

To connect, find the method you're connecting with below and follow the instructions in that section.

Option 1: mysql-connector-python

  1. Install mysql-connector-python:
pip install mysql-connector-python
  1. Copy the contents of mysql-connector-python.py into your connection file.
  2. Replace the placeholders for HOSTNAME, DATABASE, USERNAME, and PASSWORD with the copied values from the previous section. We encourage you to move these placeholder values into your .env file.
  3. To ensure a secure connection, you must fill in the SSL certificate path. This is the value for ssl_ca in the config 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.

Option 2: MySQLdb

Note: If you're developing on Windows, we recommend using mysql-connector-python instead of mysqlclient as it is easier to enable SSL.

  1. Install mysqlclient:
pip install mysqlclient
  1. Copy the contents of mysql.py into your connection file.
  2. Replace the placeholders for HOSTNAME, DATABASE, USERNAME, and PASSWORD with the copied values from the previous section. We encourage you to move these placeholder values into your .env file.
  3. To ensure a secure connection, you must fill in the SSL certificate path. This is the value for ca in the ssl 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.

Option 3: Django

  1. Copy the contents of django/my.cnf into your connection file. This may be a .env file.
  2. Replace the placeholders for HOSTNAME, DATABASE, USERNAME, and PASSWORD with the copied values from the previous section..
  3. 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.

  1. You can update your settings.py file to read from your connection file, as shown in our example.
  2. (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.

More resources

Django resources

Next steps