我想将 postgresql 连接到我的 django 应用程序,我刚刚下载了 postgresql。我按照网上的说明做了一切:我改变了settings.py
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': '12345678',
'HOST': '127.0.0.1',
'PORT': '',
}
}
创建了一个数据库test
和一个国家表,填写了它,以不同的方式更改了它,正如他们在互联网上所写的那样,该文件var/lib/pgsql/data/pg_hba.conf
创建了一个用户,授予了数据库的权限,出现了一个 Fedora 错误:
Traceback (most recent call last):
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection
self.connect()
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 256, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/postgresql/base.py", line 332, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/psycopg/connection.py", line 119, in connect
raise last_ex.with_traceback(None)
psycopg.OperationalError: connection failed: connection to server at "127.0.0.1", port 5432 failed: ВАЖНО: пользователь "postgres" не прошёл проверку подлинности (Ident)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/azat/Документы/test/manage.py", line 22, in <module>
main()
File "/home/azat/Документы/test/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 118, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/migrations/loader.py", line 58, in __init__
self.build_graph()
File "/home/azat/.local/lib/python3.12/site-packages/django/db/migrations/loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/migrations/recorder.py", line 89, in applied_migrations
if self.has_table():
^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/migrations/recorder.py", line 63, in has_table
with self.connection.cursor() as cursor:
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 320, in cursor
return self._cursor()
^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 296, in _cursor
self.ensure_connection()
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 278, in ensure_connection
with self.wrap_database_errors:
File "/home/azat/.local/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection
self.connect()
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/base/base.py", line 256, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/django/db/backends/postgresql/base.py", line 332, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/azat/.local/lib/python3.12/site-packages/psycopg/connection.py", line 119, in connect
raise last_ex.with_traceback(None)
django.db.utils.OperationalError: connection failed: connection to server at "127.0.0.1", port 5432 failed: ВАЖНО: пользователь "postgres" не прошёл проверку подлинности (Ident)
pg_hba.conf
:
...
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all ::1/128 trust
sudo -u postgres psql -U postgres -d postgres
使用命令SELECT * FROM "countries";
一切正常
我可能没有注意到,但是该表称为“国家”,并且数据库显然是 postgres
\l
Список баз данных
Имя | Владелец | Кодировка | Провайдер локали | LC_COLLATE | LC_CTYPE | локаль ICU | Правила ICU | Права доступа
-----------+----------+-----------+------------------+-------------+-------------+------------+-------------+-----------------------
postgres | postgres | UTF8 | libc | ru_RU.UTF-8 | ru_RU.UTF-8 | | |
template0 | postgres | UTF8 | libc | ru_RU.UTF-8 | ru_RU.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | ru_RU.UTF-8 | ru_RU.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
test | postgres | UTF8 | libc | ru_RU.UTF-8 | ru_RU.UTF-8 | | | =Tc/postgres +
| | | | | | | | postgres=CTc/postgres+
| | | | | | | | psqluser=CTc/postgres
\du+
Список ролей
Имя роли | Атрибуты | Описание
----------+-------------------------------------------------------------------------+----------
postgres | Суперпользователь, Создаёт роли, Создаёт БД, Репликация, Пропускать RLS |
psqluser | |
当我在控制台中运行 psql 时,它会一直工作 ,直到我指定主机-h
(尝试了 localhost 和 127.0.0.1)
尽管:
pg_isready -h 127.0.0.1 -p 5432
127.0.0.1:5432 - принимает подключения
没有任何帮助请告诉我