You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FlaskChat项目运行及PostgreSQL数据库初始化报错求助

FlaskChat项目数据库初始化认证错误求助

背景与已解决问题

我正在使用Flask开发一款私人信使,参考Github上的FlaskChat项目。在Ubuntu 22.04系统中执行flask run时曾遇到以下错误:

ImportError: cannot import name 'escape' from 'jinja2' (/home/home/.local/lib/python3.10/site-packages/jinja2/__init__.py)

当时通过pip3 install -r requirements.txt安装的依赖如下:

Flask_SQLAlchemy==2.4.1
Flask_JWT_Extended==3.24.1
Flask_SocketIO==4.2.1
Flask_RESTful==0.3.8
Flask_Migrate==2.5.3
Flask==1.1.1
Flask_Cors==3.0.8
Werkzeug==0.15.4
SQLAlchemy==1.3.5

尝试过相关解决方案但无效,该问题已于2023年11月3日解决。

当前问题:数据库初始化认证失败

现在flask run和npm run serve均可正常运行,但项目README.md的数据库部分说明不清晰。尝试初始化数据库时出现如下错误:

$ python3
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from database import init_db
>>> init_db()
Traceback (most recent call last):
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
    with util.safe_reraise():
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
    raise value
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 451, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/home/Documents/doctor_jha/flask/FlaskChat/api/database.py", line 15, in init_db
    Base.metadata.create_all(bind=engine)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/sql/schema.py", line 4286, in create_all
    bind._run_visitor(
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2032, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2024, in _optional_conn_ctx_manager
    with self._contextual_connect() as conn:
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2226, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
    Connection._handle_dbapi_exception_noconnection(
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 399, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
    raise value.with_traceback(tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
    with util.safe_reraise():
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 154, in reraise
    raise value
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 451, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/home/.local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "postgres"

(Background on this error at: http://sqlalche.me/e/e3q8)
>>>

已尝试过相关认证问题的解决方案,但均无效,恳请帮助。


内容的提问来源于stack exchange,提问作者pradeep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:25:55