如何在 Django 中访问远程 PostgreSQL 数据库服务器(只读事务)?
How to access a remote PostgreSQL database server (Read only transaction) in Django?
我有一个只读事务的 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
如何修复错误?
您需要确保您的会话没有使用数据库(默认)。我会考虑将 'default' 留给可写数据库,然后添加一个 'readonly' 数据库连接作为您在其中访问的对象的替代连接。
首先你的日志说,你的迁移没有应用到你正在连接的数据库,所以先修复它。
然后我在 settings.py 中取得了以下成功
SESSION_ENGINE="django.contrib.sessions.backends.cache"
我有一个只读事务的 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
如何修复错误?
您需要确保您的会话没有使用数据库(默认)。我会考虑将 'default' 留给可写数据库,然后添加一个 'readonly' 数据库连接作为您在其中访问的对象的替代连接。
首先你的日志说,你的迁移没有应用到你正在连接的数据库,所以先修复它。
然后我在 settings.py 中取得了以下成功
SESSION_ENGINE="django.contrib.sessions.backends.cache"