【问题标题】:Error connecting to postgresql when using flask使用烧瓶时连接到 postgresql 时出错
【发布时间】:2021-06-14 21:06:43
【问题描述】:
from flask import Flask, jsonify
from flask import request
from slotscal import results
from flask_sqlalchemy import SQLAlchemy
import time
app = Flask(__name__)


app.config['SQLALCHEMY_DATABASE_URI']= 'postgresql://postgres:test123@localhost:5432/flask'

db=SQLAlchemy(app)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hamza/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1094, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "/home/hamza/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1086, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "/home/hamza/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1017, in get_engine
    return connector.get_engine()
  File "/home/hamza/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 592, in get_engine
    sa_url = make_url(uri)
  File "/home/hamza/.local/lib/python3.8/site-packages/sqlalchemy/engine/url.py", line 694, in make_url
    return _parse_rfc1738_args(name_or_url)
  File "/home/hamza/.local/lib/python3.8/site-packages/sqlalchemy/engine/url.py", line 755, in _parse_rfc1738_args
    raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'postgresql:/test123@localhost/flask'

【问题讨论】:

  • 查看文档Postgres-psycopg2 以获得正确的 URL。
  • @AdrianKlaver 同样的错误。
  • 您需要将更改添加为问题的更新。
  • 抱歉,尝试使用先决条件 sudo apt install libpq-dev python3-dev 重新安装 psycogp2 并且它有效。谢谢。你应该回答这个问题。

标签: python postgresql flask


【解决方案1】:

连接字符串必须是 libpq 连接字符串,其语法为is documented in the PostgreSQL documentation

如果使用 URL 形式,则必须遵守 RFC 1738。特别是,postgresql: 后面需要两个斜杠:

postgresql://test123@localhost/flask

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2015-04-09
    • 2020-10-31
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多