简体   繁体   English

如何在Django中访问远程PostgreSQL数据库服务器(只读事务)?

[英]How to access a remote PostgreSQL database server (Read only transaction) in Django?

I have a PostgreSQL database server which is read only transaction. 我有一个PostgreSQL数据库服务器,它是只读事务。 I need to extract some from this database in Django. 我需要从Django这个数据库中提取一些内容。 My setting.py file is given below: 我的setting.py文件如下:

{
  {
    DATABASES = {
      'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'supercaller',
        'USER' : 'chandra_m',
        'PASSWORD' : 'XXXXX',
        'HOST': 'xx.xxx.xxx.xxx',
        'PORT': '5432',
      }
    }
  }
}

I am running the server with python manage.py runserver . 我正在使用python manage.py runserver运行服务器。 I am getting following error 我收到以下错误

  System check identified no issues (0 silenced).
  Unhandled exception in thread started by <function wrapper at 0x1aa9230>
  Traceback (most recent call last):
   File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-   
   packages/django/utils/autoreload.py", line 222, in wrapper
   fn(*args, **kwargs)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site - 
  packages/django/core/management/commands/runserver.py", line 107, in inner_run
    self.check_migrations()
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/core/management/commands/runserver.py", line 159, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/migrations/executor.py", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/migrations/loader.py", line 48, in __init__
    self.build_graph()
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/migrations/loader.py", line 179, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/migrations/recorder.py", line 53, in ensure_schema
    editor.create_model(self.Migration)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/backends/schema.py", line 270, in create_model
    self.execute(sql, params)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/backends/schema.py", line 98, in execute
    cursor.execute(sql, params)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site- 
  packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site- 
  packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/birendra/knowlarity/chandra/NodetoDjango/venv/local/lib/python2.7/site-
  packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  django.db.utils.InternalError: cannot execute CREATE TABLE in a read-only transaction

However, I am able to access this database server through a shell prompt with 但是,我能够通过shell提示符使用以下命令访问此数据库服务器:

psql -h 46.xxx.xxx.52 supercaller chandra_m

How to fix the errors? 如何解决错误?

you need to make sure your sessions are not using databases (default). 您需要确保您的会话未使用数据库(默认)。 I would consider leaving the 'default' to a writeable databaes, then add a 'readonly' database connection as an alternative connection for the objects that you are accessing in it. 我会考虑将“默认”保留为可写的数据库,然后添加“只读”数据库连接作为您在其中访问的对象的替代连接。

First your log says, you have migrations that's not applied to the db you are connecting to, so fix it first. 首先,您的日志说,您的迁移不适用于您要连接的数据库,因此请首先对其进行修复。

Then I had success with the following in settings.py SESSION_ENGINE="django.contrib.sessions.backends.cache" 然后,我在settings.py SESSION_ENGINE="django.contrib.sessions.backends.cache"成功完成了以下操作

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

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