将Django开发数据库从默认SQLite更改为PostgreSQL [英] Changing Django development Database from the default SQLite to PostgreSQL

查看:146
本文介绍了将Django开发数据库从默认SQLite更改为PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从默认的SQLite数据库迁移到Postgres数据库需要采取什么步骤?

What are the steps I need to take to migrate from the default SQLite database to Postgres database?

我正在这样做是为了使本地开发环境尽可能接近

I'm doing this to get my local development environment as close to my live server (which uses postrgres).

还是本地开发使用SQLite的原因?不建议使用Postgres进行本地开发吗?

Or is there a reason why local development uses SQLite? Is it not recommended to use Postgres for local development?

推荐答案

您可以尝试以下步骤:




1.安装psycopg2以配置数据库:

You can try the following steps:

1. Install psycopg2 to configure the database:

pip install psycopg2



2.在默认的 settings.py

更改原始值:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

收件人:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'NAME_OF_DB',
        'USER': 'DB_USER_NAME',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',
        'PORT': 'PORT_NUMBER',
    }
}



3.米格为数据库评分:


3. Migrate the DB:

python manage.py makemigrations
python manage.py migrate



编辑:
感谢@ robotHamster评论。以下是同步现有数据的方法:


Thanks @robotHamster comment. Here is the method to sync the existing data:

首先备份数据:

python manage.py dumpdata > datadump.json

更改数据库设置后:

python manage.py loaddata datadump.json

< hr>
来源:将Django DB从SQLite迁移到MySQL的最佳方法是什么?

这篇关于将Django开发数据库从默认SQLite更改为PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆