我正在尝试 create_all() for postgresql table 但我遇到了奇怪的错误
I am trying to create_all() for a postgresql table but i am getting weird errors
我的 python 烧瓶代码 (quotes.py) 是:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2:// postgres:password@localhost/quotes'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Favquotes(db.Model):
id = db.Column(db.Integer, primary_key = True)
author = db.Column(db.String(30))
quote = db.Column(db.String(2000))
@app.route('/')
def index():
result = Favquotes.query.all()
return render_template('index.html', result = result)
@app.route('/quotes')
def quotes():
return render_template('quotes.html')
@app.route('/process', methods = ['POST'])
def process():
author = request.form['author']
quote = request.form['quote']
quotedata = Favquotes(author = author , quote = quote)
db.session.add(quotedata)
db.session.commit()
return redirect(url_for('index'))
出现以下错误:
>>> from quotes import db
>>> db.create_all()
Traceback (most recent call last):
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3250, in _wrap_pool_connect
return fn()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 310, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 868, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 476, in checkout
rec = pool._do_get()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 146, in _do_get
self._dec_overflow()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 143, in _do_get
return self._create_connection()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 256, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 371, in __init__
self.__connect()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 666, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 661, in __connect
self.dbapi_connection = connection = pool._invoke_creator(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\create.py", line 590, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\default.py", line 597, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user " postgres"
我的 pip 列表包含:
(venv) PS C:\Users\KIIT\Desktop\fav-quotes> pip list
Package Version
---------------- -------
- click 8.0.3
- colorama 0.4.4
- Flask 2.0.2
- Flask-SQLAlchemy 2.5.1
- greenlet 1.1.2
- gunicorn 20.1.0
- itsdangerous 2.0.1
- Jinja2 3.0.3
- MarkupSafe 2.0.1
- pip 22.0.3
- psycopg2 2.9.3
- python-dotenv 0.19.2
- setuptools 47.1.0
- SQLAlchemy 1.4.31
- Werkzeug 2.0.2
如您所见,我正在使用虚拟环境。
错误是自我解释的;
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user " postgres"
我想,用户名中有一个额外的空白字符: postgres
,因此您应该将其删除:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:password@localhost/quotes'
尝试使用这个
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DBNAME}"
在你的情况下它将是:
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://postgres:password@localhost/quotes"
我的 python 烧瓶代码 (quotes.py) 是:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2:// postgres:password@localhost/quotes'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Favquotes(db.Model):
id = db.Column(db.Integer, primary_key = True)
author = db.Column(db.String(30))
quote = db.Column(db.String(2000))
@app.route('/')
def index():
result = Favquotes.query.all()
return render_template('index.html', result = result)
@app.route('/quotes')
def quotes():
return render_template('quotes.html')
@app.route('/process', methods = ['POST'])
def process():
author = request.form['author']
quote = request.form['quote']
quotedata = Favquotes(author = author , quote = quote)
db.session.add(quotedata)
db.session.commit()
return redirect(url_for('index'))
出现以下错误:
>>> from quotes import db
>>> db.create_all()
Traceback (most recent call last):
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3250, in _wrap_pool_connect
return fn()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 310, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 868, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 476, in checkout
rec = pool._do_get()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 146, in _do_get
self._dec_overflow()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 143, in _do_get
return self._create_connection()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 256, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 371, in __init__
self.__connect()
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 666, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\pool\base.py", line 661, in __connect
self.dbapi_connection = connection = pool._invoke_creator(self)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\create.py", line 590, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\sqlalchemy\engine\default.py", line 597, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Users\KIIT\Desktop\fav-quotes\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user " postgres"
我的 pip 列表包含:
(venv) PS C:\Users\KIIT\Desktop\fav-quotes> pip list
Package Version
---------------- -------
- click 8.0.3
- colorama 0.4.4
- Flask 2.0.2
- Flask-SQLAlchemy 2.5.1
- greenlet 1.1.2
- gunicorn 20.1.0
- itsdangerous 2.0.1
- Jinja2 3.0.3
- MarkupSafe 2.0.1
- pip 22.0.3
- psycopg2 2.9.3
- python-dotenv 0.19.2
- setuptools 47.1.0
- SQLAlchemy 1.4.31
- Werkzeug 2.0.2
如您所见,我正在使用虚拟环境。
错误是自我解释的;
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user " postgres"
我想,用户名中有一个额外的空白字符: postgres
,因此您应该将其删除:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:password@localhost/quotes'
尝试使用这个
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DBNAME}"
在你的情况下它将是:
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://postgres:password@localhost/quotes"