# Migrar SQLite para PostgreSQL (no Django)

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Esse artigo não é um tutorial, são anotações pessoais.</div>
</div>

1 - Certificar-se que não tem nenhuma migração pendente.

```powershell
python manage.py makemigrations
python manage.py migrate
```

2 - Gerar um arquivo JSON que puxa os dados do atual bd (ex: dumpdata.json)

```powershell
python manage.py dumpdata > dumpdata.json
```

**<mark>Importante:</mark>** mudar o encoding do arquivo para UTF-8.

4 - Instalar o psycopg2

```powershell
pip install psycopg2
```

5 - Ir no settings.py do projeto e colocar os dados do bd:

```python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'test',
        'USER': 'postgres',
        'PASSWORD': 'senha',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
```

6 - Sincronizar o banco de dados

```powershell
python manage.py migrate --run-syncdb
```

7 - Limpar os dados que vem no bd por padrão

```powershell
python manage.py shell
```

```powershell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
exit()
```

8 - Carregar os dados do arquivo JSON no banco de dados.

```powershell
python manage.py loaddata datadump.json
```

9 - Checar no bd se migrou tudo certo.
