Integrating Postgress Database to Flask using SQLAlchemy

Israel Aminu
3 min readJul 13, 2020

--

I was facing a lot of issues adding a Database of my flask application, there were so many tutorials and resources I found online but it didn't solve my problem. I was able to do it and this was my approach.

Photo by Campaign Creators on Unsplash

To start, this are the packages you’ll need to install in your virtual environment.

Flask-SQLAlchemy: This package is an Object Relational Mapper (ORM) for flask, it is a layer between object-oriented Python and the database schema of Postgres or any relational database.

Flask-Migrate: It’s an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are made available through the Flask command-line interface or through the Flask-Script extension.

Flask-Script: The extension provides support for writing external scripts in Flask. This includes running a development server, a customised Python shell, scripts to set up your database, cronjobs, and other command-line tasks that belong outside the web application itself.

Alembic: This package helps to manage migrations with SQLAlchemy in Python.

psycopg2: psycopg2 is a DB API 2.0 compliant PostgreSQL driver.

You can install all the packages in your virtual environment using the command below:

pip install -r requirements.txt

Working Directory:

.
├── app.py
├── manage.py
├── models.py
└── requirements.txt
0 directories, 4 files

app.py:

In my app.py file:

  • I imported the flask class and initialize an instance which I named app.
  • After that, I configured the flask app by providing the SQLALCHEMY_DATABASE_URI, which is, in this case, your Postgress server URI. This could be locally from your computer or from a particular server. You can read about this here.
  • Then I set the ‘SQLALCHEMY_TRACK_MODIFICATIONS’ to True, what this does is that it displays changes that we make whenever we alter our Database.

models.py:

In the models.py I imported the flask_sqlalchemy library and the datetime library, what I simply did was created a new database class in my models called User, we’re going to use this to verify if the user and the schema exist in our database.

manage.py:

The manage.py, this is the most important file, this helps us with database migrations whenever we make migration in our models.py file.

Finally, run database migrations and upgrades with the following command:

python manage.py db init

This will create a folder called migrations with alembic.ini and env.py files and a sub-folder migrations which will include your future migrations. It has to be run only once.

Then run migrations:

python manage.py db migrate

This generates a new migration in the migrations folder. The file is pre-filled based on the changes detected in our models.py file.

And if everything goes well, you have successfully added a Postgress database to your flask application.

Thanks for reading 😊

--

--