简体   繁体   English

Django - 连接到现有的 RDS postgres

[英]Django - Connecting to existing RDS postgres

I am trying to make a Django app to connect to an existing RDS postgres instance我正在尝试制作一个 Django 应用程序来连接到现有的 RDS postgres 实例

I have changed my settings.py file to the following:我已将settings.py文件更改为以下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': <db-name>,
        'USER': <username>,
        'PASSWORD': <pswd>,
        'HOST': 'project-hash.us-east-1.rds.amazonaws.com',
        'PORT': '5432',
    }
}

After creating Django project, running python manage.py migrate yelds:创建 Django 项目后,运行python manage.py migrate yelds:

Tracking file by folder pattern:  migrations
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying auth.0001_initial...Traceback (most recent call last):
  File "/home/gustavo.figueiredo/anaconda3/envs/lp_admin/lib/python3.7/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.DuplicateTable: relation "auth_permission" already exists

manage.py migrate will always fail if it tries to run a migration and finds that any action it's attempting to perform -- in this case, create a table to store user permission data -- has already been done.如果manage.py migrate尝试运行迁移并发现它尝试执行的任何操作 - 在这种情况下,创建一个表来存储用户权限数据 - 已经完成,它总是会失败。

If the database instance already exists, you would be much better served making use of manage.py inspectdb to create models from the database , rather than using manage.py migrate to apply models to the database.如果数据库实例已经存在,您最好 使用manage.py inspectdb从数据库创建模型,而不是使用manage.py migrate将模型应用于数据库。

If your database is an existing Django database, and you're looking to just get the migrations up and running, the simplest way is to run python manage.py migrate --fake-initial to mark the migration as having been applied without actually performing any database operations other than bumping the migration version.如果您的数据库是现有的 Django 数据库,并且您只想启动并运行迁移, 最简单的方法是运行python manage.py migrate --fake-initial将迁移标记为已应用,而无需实际执行除了提升迁移版本之外的任何数据库操作。

psycopg2.errors.DuplicateTable: relation "auth_permission" already exists psycopg2.errors.DuplicateTable:关系“auth_permission”已经存在

You do not have a problem connecting.您没有问题连接。 To get this error, you must already be connected.要获得此错误,您必须已连接。

The problem is that the migration you are trying to run is incompatible with the database you are connected to.问题是您尝试运行的迁移与您连接的数据库不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM