Adventures in Machine Learning

Streamline Django Project Deployment with Heroku Amazon S3 and Fabric

Converting a Local Django Project from MySQL to Postgres

If you have been working on a Django project that uses a MySQL database, you know that sometimes you need to switch to Postgres. This can happen because Postgres is more scalable, faster, and provides better transaction management compared to MySQL.

Whatever the reason may be, switching between databases can seem a daunting task. But don’t worry, follow these steps, and you’ll have your Django project running on Postgres in no time!

Installing Dependencies

Before you start migrating, make sure that you have the necessary dependencies installed. You’ll need the psycopg2 package that provides a PostgreSQL database adapter for Python.

To install it, you can use pip, the Python package installer:


$ pip install psycopg2


Another handy tool is py-mysql2pgsql, which automates most of the migration process. You can install it using pip as well:


$ pip install py-mysql2pgsql


Setting up a Postgres Database

Once you have the necessary packages installed, you can start setting up the Postgres database. First, make sure that Postgres is running on your system, and you have the `psql` command-line tool installed.

If you’re using macOS, you can install Postgres using Homebrew:


$ brew install postgresql


To create a PostgreSQL database, use the `psql` command-line tool. Open your terminal and type the following command:


$ psql -U


The -U switch specifies the username that you want to use to connect to the Postgres server.

If you’re using the default Postgres superuser, you can omit the -U switch. Once you’re connected, you can create a new database using the CREATE DATABASE command:




Make sure to replace `` with the name of your database.

Migrating Data

After setting up the Postgres database, you’re ready to migrate your data. Py-mysql2pgsql offers you an easy way to do that.

First, create a new file named `mysql2pgsql.yml`. This file will contain the configuration options for the migration tool.

Here’s an example:



host: localhost

user: root

password: mysql_password

database: mysql_database


host: localhost

user: postgres

password: postgres_password

database: postgres_database


Make sure to replace the values with your MySQL and Postgres database credentials. Once you have created the `mysql2pgsql.yml` file, you can run py-mysql2pgsql and pass your YAML file as an argument:


$ python dbconvert –config=mysql2pgsql.yml


This will convert your MySQL database to Postgres.


After migrating your data, you need to update your `` file to reflect the changes. Open the file and look for the `DATABASES` section.

Replace the `ENGINE` option with `django.db.backends.postgresql_psycopg2`:



‘default’: {

‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,

‘NAME’: ‘‘,

‘USER’: ‘‘,


‘HOST’: ‘localhost’,

‘PORT’: ”,




Replace ``, ``, and `` with the values that you used when creating your Postgres database.

Resync Database and Test Server

After updating your `` file, you need to resync your database. Run the following command:


$ python migrate


This’ll apply any pending migrations and synchronize your Django models with the Postgres database.

Finally, start your development server:


$ python runserver


And test your project to ensure everything is working properly. You may need to make a few more adjustments to your project, depending on any custom code or configuration you’re using.

Adding a File

If you’re a seasoned Django developer, you probably know about the benefits of having a `` file in your project. A `` file is an extension to your `` file.

The purpose of this file is to include local environment settings that differ from your production environment. For example, you may have different database settings, API keys, and debug options.

Including in .gitignore

If you have a `` file, be sure to include it in your `.gitignore` file. You don’t want to accidentally commit development settings to your production environment.

Including your `` file in your `.gitignore` file ensures that it won’t be pushed to your repository.

The One True Way

If you’re wondering about alternative patterns, don’t worry! While there isn’t just one way to approach this, the convention among Python developers is to use a `` file. It’s a handy way to keep your development environment separate from your production environment and avoid any potential conflicts.


In conclusion, by following these steps, you can switch your Django project from MySQL to Postgres effortlessly. Remember to install the necessary dependencies, create a new Postgres database, migrate your data, update your `` file, resync your database, and test your project.

And if you don’t have one already, consider adding a `` file to your project to keep your development environment separate from your production environment.

Heroku Setup

Heroku is a cloud-based platform that allows you to deploy and host your web applications. It’s a popular choice among developers because it’s easy to use, cost-effective, and reliable.

Setting up your Django project on Heroku requires a few steps, but once you’re done, you’ll have a hassle-free way to deploy, manage, and scale your application.

Purpose of Heroku

Heroku is a Platform-as-a-Service (PaaS) that provides developers with a cloud-based platform to build, deploy, manage, and scale their applications. Heroku is based on the Ruby on Rails framework and supports several other programming languages, including Python.

With Heroku, developers can focus on their application code and leave the infrastructure management to the platform. Heroku provides features like automated builds, auto scaling, easy deployment, add-ons, and collaborative development, making it an excellent choice for startups and small businesses.

Pushing to Github and Heroku

After setting up your Heroku account and installing the Heroku command-line interface (CLI), the next step is to create a new Git repository for your Django project and push it to Github. Here’s how you can do it:


Create a new repository in Github. 2.

In your terminal, navigate to the directory where your Django project is located. 3.

Initialize a new Git repository by running the following command:


$ git init


4. Add your files to the repository:


$ git add .


5. Commit your changes:


$ git commit -m “Initial commit”



Link your local repository to the Github repository:


$ git remote add origin


7. Push your code to Github:


$ git push -u origin master


Once your code is pushed to Github, you can now deploy it to Heroku.

Here’s how:

1. From the Heroku dashboard, click on the “New” button and select “Create new app.”


Give your app a name and select your region. 3.

In your terminal, log in to your Heroku account using the Heroku CLI:


$ heroku login


4. Navigate to your Django project’s directory and create a new Heroku app:


$ heroku create



Push your code to Heroku:


$ git push heroku master


6. Run migrations on Heroku:


$ heroku run python migrate



Finally, open your app by running:


$ heroku open



After deploying your Django project to Heroku, it’s crucial to test it to ensure that it’s functioning correctly. There are different ways to test your application on Heroku, but one common way is to use the Heroku logs command.

The Heroku logs command shows you all the logs coming from your application, including any errors or warnings. By running the following command, you can view the logs:


$ heroku logs –tail


This command will keep showing you the logs in real-time until you stop it.

You can use this command to debug any issues that may arise when running your application on Heroku.

Amazon S3

Amazon S3 is a cloud-based service that allows you to store and retrieve data, including static files, using a simple web service interface. With

Amazon S3, you can host your static files, such as images, videos, and CSS files, and serve them directly to your web application.


Amazon S3 reduces the load on your server and improves the loading time of your web pages. Purpose of

Amazon S3

Amazon S3 is a robust and highly scalable service that provides developers with a way to store and retrieve data in the cloud. It’s a cost-effective solution for hosting your static files, and it reduces the load on your server, making your web application faster and more responsive.


Amazon S3, you can store and access your static files from anywhere, and you don’t need to worry about data loss or hardware failure. How to Use

Amazon S3

To use

Amazon S3, you need to create an Amazon Web Services (AWS) account and create a new S3 bucket.

Here’s how:

1. Log in to your AWS account.

2. Navigate to the S3 service.

3. Click on the “Create bucket” button.

4. Give your bucket a unique name and select your region.

5. Click on the “Create bucket” button.

Once your S3 bucket is created, you can now start uploading your static files. You can either use the AWS Management Console or the AWS Command Line Interface (CLI) to upload your files.

Here’s how you can upload files using the AWS Management Console:

1. Click on your S3 bucket.

2. Click on the “Upload” button.

3. Select the files that you want to upload.

4. Click on the “Upload” button.

You can also upload files using the AWS CLI. Here’s how:


Install the AWS CLI. 2.

Open your terminal. 3.

Navigate to the directory where your files are located. 4.

Run the following command:


$ aws s3 cp s3:///


Replace `` with the path to your local file, `` with the name of your S3 bucket, and `` with the path on your S3 bucket where you want to upload your file. Once you have uploaded your files to

Amazon S3, you can use them on your web application by referencing the URLs in your HTML or CSS files.

For example, to reference an image stored in your S3 bucket, you can use a URL like this:



Replace `` with your S3 bucket name, `` with your AWS region, and `` with the path to your file in the S3 bucket.


In conclusion, deploying your Django project to Heroku and hosting your static files on

Amazon S3 are great ways to improve the performance and scalability of your web application. By following the steps outlined in this article, you can easily set up your Django project on Heroku, push your code to Github and Heroku, and test your application.

You can also use

Amazon S3 to host your static files, which will improve your web application’s loading time. With these tools, you can build and deploy your web application with confidence.


Fabric is a Python library that provides a simple way to automate deployment tasks, such as running scripts or executing commands on remote servers.

Fabric is built for the deployment process and is a great tool to automate and streamline repetitive deployment tasks.


Fabric, you can create a script to automate your deployment process and execute it with a single command. It helps to save time and effort by reducing the likelihood of human error.

Purpose of


Fabric was built by developers, for developers, with the goal of making it easier to streamline and automate the deployment process.

Fabric is designed to simplify deployment tasks by providing a simple and easy-to-use API that can perform both local and remote operations.

It’s lightweight, flexible, and extensible, providing you with the power to create complex deployment pipelines by writing simple Python scripts.

Fabric’s core philosophy is to provide a straightforward way to automate deployment tasks that would otherwise require manual intervention.



To get started with

Fabric, you first need to install it. You can install

Fabric using pip, the Python package installer:


$ pip install fabric


Once you have installed

Fabric, you can start creating your deployment script.

A deployment script is simply a Python file that includes

Fabric-specific functions that define your deployment pipeline. Here’s an example:


from fabric import Connection, task


def deploy(c, branch=”master”):

with Connection(“[email protected]”) as conn:“cd ~/project”)“git checkout {}”.format(branch))“git pull”)“source venv/bin/activate”)“pip install -r requirements.txt”)“python migrate”)“python collectstatic –noinput”)“touch ~/project/app.wsgi”)


In this example, we define a

Fabric task called `deploy`.

The task itself is decorated with the `@task` decorator, which tells

Fabric that this function is a

Fabric task. The `deploy` function connects to a remote host at `[email protected]` using the

Fabric `Connection` object.

We then run a series of commands on the remote server to run a deployment. Specifically, we change to the project directory, checkout the specified branch of our code, pull in the latest changes, create a new virtual environment, install the required dependencies, migrate the database, collect static files, and touch the WSGI file.

To execute this

Fabric script, open up your terminal and navigate to the directory where the script is located. Run the following command to execute the `deploy` function:


$ fab deploy


Fabric will connect to the remote server and execute the defined series of commands, effectively automating the deployment process. If something goes wrong during the deployment,

Fabric will stop the process and print out any errors that may have occurred, making it easy to troubleshoot and debug.


In conclusion,

Fabric is a powerful tool that you can use to streamline your deployment process and automate tedious tasks. It provides a simple and easy-to-use API that makes it easy to write complex deployment pipelines with just a few lines of code.


Fabric, you save time and effort by reducing the likelihood of human error. By following the instructions provided in this article, you can quickly get started with

Fabric and start automating your deployment processes today.

In sum, this article covers different tools that developers can use to automate and streamline their deployment process. It explains how to convert a local Django project from MySQL to Postgres using py-mysql2pgsql and the necessary dependencies.

The article also covers the importance of adding a file, and how to use Heroku to deploy and host your web application, as well as

Amazon S3, which is a cloud-based service that allows you to host your static files, avoiding server overload and improving the app’s loading speed. Finally, it explains how to use

Fabric, a Python library that provides a simple way to automate deployment tasks and streamline the deployment process, reducing human error.

By following the instructions provided in this article, developers can improve their workflows, save time, and ensure a reliable deployment process.

Popular Posts