我一直遇到无法连接到数据库的问题:
psycopg2.OperationalError: could not connect to server: No such file or directory
服务器是否在本地运行并接受
Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接?
我做了一些阅读,这似乎是一个很常见的问题,大多数人通过检查端口是否正确来解决这个问题,即在他们的 setup.py 中将 5432 更改为 5433。但这似乎不是我的问题——在/opt/bitnami/postgresql 目录中我看到 .s.PGSQL.5432。我可以登录到 psql,它似乎可以正常工作。我认为默认的数据库名称是 postgresql,但我也尝试过使用名为 djangostack 的数据库,但它也没有用。我在 Django 的 setting.py 中的数据库信息如下所示:
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2', #'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'postgres', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'bitnami', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
我查看/tmp/目录,那里没有任何东西,无论是隐藏的还是其他的。应该有吗?有谁知道我做错了什么?我愚蠢地覆盖了原来的示例项目,所以我看不到那里的设置。
非常感谢, 亚历克斯
最佳答案
所以 PostgreSQL 套接字在 /opt/bitnami/postgresql
但你的 libpq
(psycopg2
包装的 C 库)正在寻找/tmp
中的套接字,对吧?尝试更改 HOST
setting to point at the proper socket location :
'default': {
'ENGINE': 'postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres'
'PASSWORD': 'bitnami',
'HOST': '/opt/bitnami/postgresql/.s.PGSQL.5432', # <-------
'PORT': '5432',
}
Or this :
'default': {
'ENGINE': 'postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres'
'PASSWORD': 'bitnami',
'HOST': '/opt/bitnami/postgresql', # <-------
'PORT': '5432',
}
您可能还想更改 PostgreSQL 密码。
关于django - 无法使用 Bitnami Django 堆栈连接到 Postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6102232/