I have created my own Flask app locally and I am trying to host it now on Heroku. I have created the database, but I still miss the tables in it.
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://{username}:{password}#{hostname}:{port}/{databasename}".format(
username="username_given_by_heroku",
password="password_from_heroku",
hostname="hostname",
port="xxxx",
databasename="name",
I have tried adding the tables the following way in the Heroku python console:
import psycopg2
from Website.__init__ import app, db
db.create_all()
This gives the following error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "ec2-34-230-153-41.compute-1.amazonaws.com" (34.230.153.41), port 5432 failed: FATAL: password authentication failed for user "username"
connection to server at "hostname" (34.230.153.41), port xxxx failed: FATAL: no pg_hba.conf entry for host "34.205.2.34", user "username", database "database", no encryption
I have found the following link and gave the psql user a password in the PSQL shell, with no success.
Flask & Alchemy - (psycopg2.OperationalError) FATAL: password authentication failed
Requirements.txt:
Flask==2.1.1
Flask_Login==0.6.0
Flask_SQLAlchemy==2.5.1
Werkzeug==2.1.1
gunicorn==20.0.4
psycopg2==2.9.3
Models.py:
from . import db
class User(db.Model):
columns...
class otherTable(db.Model):
...
I also don't seem to have a pg_hba.conf or I can't find it.
How can I create the tables in my Heroku database?
In Heroku, you can add the PostgreSQL database as an Add-on. Just go to the resources tab on your application page in Heroku. In the Add-on search bar, search for "postgres" and select "Heroku Postgres". After selecting, click on "Attach as database". This will add the "DATABSE_URL" as a config variable in Heroku.
In your python app write the following code:
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
if SQLALCHEMY_DATABASE_URI.startswith("postgres://"):
SQLALCHEMY_DATABASE_URI = SQLALCHEMY_DATABASE_URI.replace(
"postgres://", "postgresql://", 1)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
Related
I am new to Flask.
I have a task to connect a PostgreSQL database to my Flask app and create some API endpoints.
All the details I have on the database are the following:
Database location: postgres://candidate.company.org/company
Username: interview
Password: cake
To connect to the database I did the following:
myproject/myapp.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://interview:cake#candidate.company.org/suade'
db = SQLAlchemy(app)
I think it is connected (how can I be sure it is connected?).
My problem is that in the task it is not written what the database contains or what the name of tables are.
How can I find out the name and content of the tables in the database if I do not know anything about it?
Try query something like: db.session.query("1").from_statement("SELECT 1").all()
password authentication failed for user "saleor"
FATAL: password authentication failed for user "saleor"
when migrating the first time
if it's something related to database please someone help me to figure this out
I'm using Postgres as the saleor documentation says but I can't pass this migrations point
I read somewhere I have to create a database called saleor with saleor as a user with password 'saleor' who is a superuser and if that is the solution tell me how to do that
DATABASES = {
"default": dj_database_url.config(default="postgres://postgres:12345#localhost:5432/saleor", conn_max_age=600)
}
Postgres is my username, and 12345 is my password for username i.e postgres and saleor is database name. you can change database name to any. If you did not change username,password, and database name the it takes default one.
Step 1: Remove everything inside migration folder for app, except the init.py file.
Step 2: Delete or Truncate Your Current Database which is 'saleor'
Step 3: Create the initial migrations and generate the database schema using folowing commands
python manage.py makemigrations
python manage.py migrate
Step 4: Now you can Create new Super user using following command
python manage.py createsuperuser
I have a Django application that is working perfectly. It's connected to a MySQL server hosted on the cloud.
The MySQL server is set to auto-rotate the password every 30 days for security reasons. Django can access the new password only when settings.py is loaded using a custom function I have developed (that will fetch the new password from AWS Secrets Manager).
I'm looking for a way to allow Django to detect if a connection has a problem then update the password all transparent to the user.
Is that possible?
Options I can think of:
You could use a custom middleware that checks the connection before each request.
You could use a cron job that periodically checks for failed database connections, and updates the settings if it finds such a failure.
To check for connections you can use the method django.db.connection.ensure_connection(). If you look at this answer, you could try something like this:
from django.db import connection
from django.db.utils import OperationalError
class Command(BaseCommand):
def handle(self, *args, **options):
try:
# if this succeeds, no further action is needed
connection.ensure_connection()
except OperationalError:
# update the DB password and try again
I am trying to connect my flask app to my prostgreSQL db, and i find this configuration example (below code). I just do not know how to find my postgreSQL URI
app = Flask(__name__)
#how do i know my postgresql URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/pre-registration'
db = SQLAlchemy(app)
From the documentation of SQLAlchemy (http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls):
The typical form of a database URL is:
dialect+driver://username:password#host:port/database
This means that, if you have a Postgres DB called my_db running on localhost on port 5432, accessed by username user and password pass, your URL will look like:
postgresql://user:pass#localhost:5432/my_db
Is there a standard set of privileges that should be given to the user used to access a Flask SQLAlchemy database. For example with
application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://app#127.0.0.1/test'
db = flask.ext.sqlalchemy.SQLAlchemy(application)
what privileges should app be given?