db.create_all() 'NoneType' object has no attribute 'drivername'

I think this is an issue with how you're attempting to connect to your Postgres database:

app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("postgresql://postgres:password@localhost/database1")

you probably want this line to be the following instead:

app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost/database1"

since the os.getenv(...) is currently trying to get an environment variable on your system named: "postgresql://postgres:password@localhost/database1" and you surely didn't set up an environment variable with this name.. Which is why you're getting a NoneType error for your postgres driver:

AttributeError: 'NoneType' object has no attribute 'drivername'.

If you want to use an environment variable to get your database connection string, do something like the following in your .bash_profile or .bashrc file:

export SQLALCHEMY_DATABASE_URI='postgresql://postgres:password@localhost/database1'

then change your database connection code to the following:

app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('SQLALCHEMY_DATABASE_URI')

Hopefully that makes sense!


To clarify gbajson's answer, os.getenv gets a value from a specific environment variable. You either need to store the database URI in an env var (before you start Flask) and get it from there:

app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URI")

or, hard code it directly as a string without using getenv:

app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost/database1"