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

时间:2015-01-05 11:03:17

标签: django postgresql

我有一个PostgreSQL数据库服务器,它是只读事务。我需要从Django中的这个数据库中提取一些。我的setting.py文件如下:

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

我正在使用python manage.py runserver运行服务器。我收到了以下错误

  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

但是,我可以通过带有

的shell提示符访问此数据库服务器
psql -h 46.xxx.xxx.52 supercaller chandra_m

如何解决错误?

2 个答案:

答案 0 :(得分:2)

您需要确保您的会话不使用数据库(默认)。我会考虑将'default'保留为可写数据库,然后添加一个'readonly'数据库连接作为您正在其中访问的对象的替代连接。

答案 1 :(得分:0)

首先,您的日志显示,您的迁移未应用于您要连接的数据库,因此请先修复它。

然后我在settings.py中获得了以下成功  SESSION_ENGINE="django.contrib.sessions.backends.cache"