将 Django 连接到 PostgreSQL 数据库 (GeoDjango)
Posted
技术标签:
【中文标题】将 Django 连接到 PostgreSQL 数据库 (GeoDjango)【英文标题】:Connecting Django to PostgreSQL database (GeoDjango) 【发布时间】:2021-12-31 09:52:25 【问题描述】:我在使用 Django 网站连接到我的 Postgresql 数据库时遇到问题。我的数据库设置在 5434 端口,因为默认 5432 端口中已经存在一个数据库。
这是我遇到的错误
Traceback (most recent call last):
File "c:\users\anouphong\appdata\local\programs\python\python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "c:\users\anouphong\appdata\local\programs\python\python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\core\management\commands\runserver.py", line 127, in inner_run
self.check_migrations()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\core\management\base.py", line 505, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\loader.py", line 223, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 270, in cursor
return self._cursor()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 246, in _cursor
self.ensure_connection()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
self.connect()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
self.connect()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\postgresql\base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
这是我在 django 中的 settings.py 文件
DATABASES =
'default':
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'database_name',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': 'localhost',
'PORT': '5434',
嗨,我想通了。真是愚蠢的错误。我添加了两个 Database= 配置两次,一次在文件顶部,一次在底部,并且必须删除它,哈哈。
【问题讨论】:
尝试使用您的数据库设置通过 psycopg2 直接连接:psycopg.org/docs/module.html#psycopg2.connect 不过,很奇怪,django 正在尝试连接 5432。您是否有多个具有相同DEFAULT_DB_ALIAS
的数据库(即 '默认')?
【参考方案1】:
此问题是由于缺少权限而产生的。您应该在 pg_hba 文件中授予权限。
登录到您的数据库用户,然后播放命令psql
。下面给出了顺序命令。
sudo su - postgres
psql
show hba_file;
在我的例子中,输出是/var/lib/postgresql/data/pg_hba.conf
。记住这个位置
\q
ctrl + d # for loging out from postgres user
sudo vim /var/lib/postgresql/data/pg_hba.conf #you can use your prefer text editor
您需要更改文件,如下所示。只需将所有方法更改为 trust。
【讨论】:
这是核心选项:“当指定信任身份验证时,PostgreSQL 假定任何可以连接到服务器的人都有权使用他们指定的任何数据库用户名(甚至是超级用户名)访问数据库。 " postgresql.org/docs/current/auth-methods.html 哈哈 @CoffeeBasedLifeform 此设置用于开发而非生产。如果您想在生产中运行此设置,则会导致安全问题。对于生产,只需将 IP 或主机添加到此列表中。对于开发来说没关系。 这只是不必要的(并且会滋生坏习惯)。他们通过 IP4 在本地连接,默认的身份验证方法 iirc 是密码身份验证md5
。他们有密码,应该可以正常连接。如果他们无法连接,因为他们的设置指定了错误的密码,那么将所有内容设置为trust
就是绕过问题而不修复它。以上是关于将 Django 连接到 PostgreSQL 数据库 (GeoDjango)的主要内容,如果未能解决你的问题,请参考以下文章
Postgresql 用户未连接到数据库(Nginx Django Gunicorn)
连接到 Django 后端的 AWS Postgres 实例
设置 PostgreSQL,无法连接到数据库 postgres
无法使用 Postgresql 将 JDBC 连接到 sonarqube