django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录
Posted
技术标签:
【中文标题】django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录【英文标题】:django.db.utils.OperationalError: could not connect to server: No such file or directory 【发布时间】:2015-12-24 15:30:50 【问题描述】:我在settings.py中的数据库设置如下
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT':'8081',
但是当运行 python manage.py migrate 我收到错误 django.db.utils.OperationalError: could not connect to server: No such file or directory
Traceback (most recent call last):
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.8081"?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 93, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/executor.py", line 19, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/loader.py", line 182, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/vivekpradhan/joker/basket/local/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.8081"?
任何帮助都会有所帮助,在此先感谢 我正在开发一个 ubantu 操作系统。
【问题讨论】:
您确定postresql
正在您的服务器上运行吗?
不确定。我是 django 新手,第一次将我的 db 设置从 sqllite 更改为 postgresql。
您可能应该在您的设备上安装并运行 postgresql 服务器。
我已经安装好了
【参考方案1】:
这些步骤对我有用 如果有,请停用 virtualenv。
pyenv 停用
安装所需的库。
sudo apt-get install libpq-dev python-dev
安装 PostgreSQL。
sudo apt-get install postgresql postgresql-contrib
启动 psql shell。
sudo -u postgres psql
通过输入以下命令设置 postgres 用户密码,系统将在 psql shell 中提示您输入密码。完成后按 Control + D 退出。
\密码postgres
创建一个名为 django_user 的新数据库用户。
`
sudo -u postgres createuser django_user
再次进入 psql shell。
sudo -u postgres psql
创建数据库并将其命名为 django_db。
创建数据库 django_db;
设置 django_user 的密码。
ALTER USER django_user WITH PASSWORD '';
正确授予权限。
将数据库 django_db 上的所有权限授予 django_user;
退出 psql shell 并激活 virtualenv。
pyenv 激活
安装 psycopg2 包。
pip 安装 psycopg2
如下编辑 Django 项目 settings.py。
数据库 = '默认': '引擎': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_db', '用户': 'django_user', '密码': '', '主机':'本地主机', '端口':'5432',
初始化数据库。
python manage.py 迁移
启动 Django 项目,看看是否一切正常。
python manage.py runserver 0.0.0.0:8000
完成 =)
【讨论】:
【参考方案2】:看起来 PostgreSQL 根本没有运行。尝试运行
sudo service postgresql start
【讨论】:
你是怎么安装的? "pip install psycopg2" in my virtualenv 您需要安装实际的 Postgresql 服务器。 Psycopg2 只是一个 Python 适配器,可帮助您连接到它。请尝试help.ubuntu.com/community/PostgreSQL 获取安装指南。以上是关于django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录的主要内容,如果未能解决你的问题,请参考以下文章