Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to create_all() for a postgresql table but i am getting weird errors

My python flask code (quotes.py) is :

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'))

Getting the following errors :

>>> 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"

my pip list contain :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(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

and as you can see i am using a virtual environment.

>Solution :

Try to use this

 app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DBNAME}"

in your case it would be :

 app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://postgres:password@localhost/quotes"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading