The basic template of a flask application with postgres database integration.
Frequently desired for a Heroku deployment.
- Clone the repo.
$ git clone git@github.com:sharonzhou/flask-postgres.git
- Enter the glorious directory!
$ cd flask-postgres/
-
Install libraries.
Ideally, into a virtual environment, e.g. with virtualenv or conda, but that is not required.
$ pip install -r requirements.txt
-
Create a postgres database. We will link it to the flask app in the subsequent steps.
Here's how to do it on a Mac with Homebrew, with a database named my_practical_db_name, which you can replace with a more practical database name.
$ brew services restart postgresql
$ createdb my_practical_db_name
- Get your database username. Below, it's my_cool_username.
$ psql my_practical_db_name
my_practical_db_name=# select current_user;
current_user
--------------
my_cool_username
my_practical_db_name=# \q
-
Link the postgres database to the Flask app.
The link to your database includes the database type (postgres), your username (my_cool_username), the host (localhost, aka. 127.0.0.1), and your database name (my_practical_db_name).
$ export DATABASE_URL="postgresql://my_cool_username@localhost/my_practical_db_name"
- Start the server.
$ python run.py
-
Open http://0.0.0.0:5000 in your browser.
-
Check if your postgres database is integrated correctly by clicking: Add a "Thing" to your database.
-
Modify this template to your project.
You can begin by adding styles to the placeholder CSS file living at app/static/css/app.css, and adding scripts to the placeholder Javascript file living at app/static/js/app.js.