quickly setup and deploy a flask app with postgres to DigitalOcean app platform with GitHub integrations
Download this project and follow the steps below.
- download or fork the project
- open terminal and cd into project folder
- run following commands:
python3 -m venv venv
source venv/bin/activate
pip install flask
pip install -r requirements.txt
- rename
.env.sample
to.env
- setup local database named
doappplatform
or you can call it whatever you want, just make sure you change theconfig.py
file to match. user:postgres
, pass:postgres
.
- run command in terminal:
python app.py
- add a faq, make sure local db connection works
- create repo with project files
- push to your github account
- go to digitalocean.com, login, click Create button and select Apps
- select github as the provider and then find your repo and select it. select settings you prefer.
- click add a resource on the next page and then select database. name it, create it and attach it to the project
- setup environment variables on the next screen. you will need to setup the following env variables:
FLASK_ENV
=Production
FLASK_DEBUG
=False
SQLALCHEMY_DATABASE_URI
=get this later
SECRET_KEY
=generate_a_key
DATABASE_URL
= this is autogenerated by digitalocean, just leave it
you can generate a secret key with the following command in the terminal: python3 -c 'import secrets; print(secrets.token_urlsafe(16))'
- publish and deploy project. let the project build and database provision. once it's done or fails to build, we need to make some additional modifications.
- to get the app to deploy successfully, the run command needs to be modified. by default, the run command is missing app:app, so it needs to be added. go to settings, select the app under components, scroll down to commands and press edit.
original command by default:
gunicorn --worker-tmp-dir /dev/shm
modified command, necessary to deploy:
gunicorn --worker-tmp-dir /dev/shm app:app
once you hit save, the build and deploy process should start over again.
- the app should build and deploy successfully, however when you open it in the browser, it will probably have an error screen. app is good but we need to go back and get database connect settings now.
- go back to settings and select the database under components just below the horizontal nav bar. Scroll down to connect details and click view. change the dropdown to connection string and copy the contents. scroll up and select the app under components and then edit the environment variable for
SQLALCHEMY_DATABASE_URI
. Paste the connection string into the value input. Press save and your app will begin rebuilding and deploying.
if you've completed all the steps above correctly, fuck ya, your flask app with postgresql should be running on DigitalOcean App Platform. Anytime you push to /main
branch, the app will rebuild and deploy automatically (unless you changed settings)