How to migrate database into production server in Django
Tags: django, python, migration, postgres
Published: 2022-05-20 06:57:31

I search a lot about how to migrate the Django model as well as whole data from the development database to production, and I found lots of explanation about it (One of the good explanations is [here](https://stackoverflow.com/a/10860413/9354344)) but did not find the easy solution. For this problem, I come up with my solution and I am explaining in this blog how to do that. Before moving to the core thing about this blog, I just want to raise one question and my thought about it.

##### Do I need to push my migration file to GitHub?

All individuals have their own perspectives. I was also confused about what is the best practice in beginning of my Django journey. After reading [this answer from](https://stackoverflow.com/a/67986271/9354344), I tried to push my migrations as well. But I think it is not a good way, so I removed the migrations from GitHub.

Okay, now lets start the core thing step by step:

### 1. Create the backup for your database

The first step is to create the database backup. In my case, I am using the PostgreSQL database. To create the backup file, I simply need to run the following command in the terminal,

```shell
pg_dump -U postgres -h localhost -d database > db_backup_2022_05_20.sql
```
> where, `postgres` is the database user, `localhost` is the database host, `database` is the database name and `db_backup_2022_05_20.sql` is the backup file name.

### 2. Migrate the Django models to the server

Since this blog is only about how to migrate the database (not about the whole procedure of how to publish the Django site into production), I am only focusing on the database migration. After pulling the code to the server, the first step is to migrate the database tables and schema only by using the Django command,

```shell
# make migrations
python manage.py makemigrations

# migrate models
python manage.py migrate
```

### 3. Migrate the data
If you want the clean database to initialize the production environment, it is a good way to go, no further action is needed. But if you want to move all the data from the developer environment to production, you need to restore the development database. To do so, if you go with the normal method, you might get an error due to a foreign key, and also duplicated data issue. But to prevent this action and restore data only, the `psql` provides a nice command line. You just need to do the following,

```shell
pg_restore -U postgres -h IP -d database --disable-triggers --data-only db_backup_2022_05_12.sql     
```
> Where, `postgres` is the database user, `ip` is the server IP address, `database` is the database name and `db_backup_2022_05_12.sql` is the backup file location. If you want to know more about the pg_restore command, please check [this official documentation](https://www.postgresql.org/docs/current/app-pgrestore.html). 

This step might throw some error messages or warnings as well, but you can simply ignore those.

### 4. What next?
If you want to update the Django model, you can first test your model in the development environment. To update those changes on production, you just need to migrate the model as below on the server,

```shell
# make migrations
python manage.py makemigrations

# migrate models
python manage.py migrate
```

> The migration file from the development and production environment will be different. If you check both databases from development and production, you will find the different data in the `django_migration` table. 

Congratulations! you finally migrated the database with all the data from the development environment to production. 

If you like this blog, please support me by subscribing to my YouTube channel: https://www.youtube.com/c/iamtekson