This is an example database app using PostgreSQL, Flask-SQLAlchemy and Flask-Migrate
You will need to ensure you install PostgreSQL on your computer. You can do so by installing PostgreSQL from the link below and following the instructions:
https://www.postgresql.org/download/
When installing PostgreSQL, select the PostgreSQL Server, pgAdmin 4 and Command Line Tools components. Ensure you deselect the Stack Builder component during the installation as it is not necessary for this course.
Create a database and ensure you have a database user that you can use to connect to the database.
To begin using this app you can do the following:
- Clone the repository to your local machine.
- Create a Python virtual environment e.g.
python -m venv venv
(You may need to usepython3
instead) - Enter the virtual environment using
source venv/bin/activate
(or.\venv\Scripts\activate
on Windows) - Install the dependencies using Pip. e.g.
pip install -r requirements.txt
. Note: Ensure you have PostgreSQL already installed and a database created. - Edit the
app/__init__.py
file and enter your database credentials and database name. - Run the migrations by typing
flask db upgrade
- Start the development server using
flask --debug run
.
I have included a separate config file app/config.py
that can be used for setting up
configuration for different environments e.g. Development and Production
Edit app/__init__.py
and uncomment the following lines:
# from .config import Config
...
# app.config.from_object(Config)
Using the separate config file will also require you to set environment variables on your local computer or server at the command line. For example on Linux or MacOS:
export SECRET_KEY="my-super-secret-key"
export DATABASE_URL="postgresql://yourusername:yourpassword@localhost/databasename"
Or on Windows:
set SECRET_KEY="my-super-secret-key"
set DATABASE_URL="postgresql://yourusername:yourpassword@localhost/databasename"
You can also create a .env
file in the root of your project and add your Environment variables there. See .env.sample
as an example. The config.py
file is already setup to automatically load the .env
file.