Install flyctl:
curl -L https://fly.io/install.sh | shflyctl postgres create
# follow the instructions, use name optimap-db, region Frankfurt or Amsterdam, configuration "Development", do not scale to 0
flyctl image show --app optimap-db
# PostGIS needs a bit more memory; THIS IS NOT FREE: https://fly.io/docs/about/pricing/?utm_campaign=oom-notification&utm_medium=email&utm_source=flyio#virtual-machines
fly status --app optimap-db
fly machine update [see output from previous command] --memory 512 --app optimap-dbNote the username, password, connection string etc. in a secure place and manually set DATABASE_URL for the app.
Note that you need to change connection type to postgis, i.e., start with postgis://.
Create database and enable PostGIS
flyctl postgres connect -a optimap-db
# in postgres=#
CREATE DATABASE optimap;
\connect optimap
CREATE EXTENSION postgis;If you want to start from scratch with an empty database, you can use
# CAREFUL!
#DROP DATABASE optimap WITH (FORCE);Using the client connection above or connect with pgAdmin4 and do the same with the UI.
Use Dockerfile instead of Django on Fly because of GIS dependencies. See also the following useful tutorials:
- https://learndjango.com/tutorials/deploy-django-postgresql-flyio
- https://fly.io/docs/languages-and-frameworks/dockerfile/
- https://dev.to/teachmetechy/django-rest-framework-on-flyio-582p
Note: See the file fly.toml for a number of environment variables and configurations that are already set!
From the interactive UI we learned:
We recommend using the database_url (
pip install dj-database-url) to parse the DATABASE_URL from os.environ['DATABASE_URL']For detailed documentation, see https://fly.dev/docs/django/
The current configuration style uses dj-database-uri.
However, because the database connection is not available when the migrations are run, this does not work rightaway.
We cannot set database connection because the app does not exist at this point.
Therefore, now set the DATABASE_URL before deploying.
flyctl secret set DATABASE_URL=DATABASE_URL=postgis://...Check the secret exists:
flyctl secrets listLaunch the app using the Dockerfile:
# only used for the first configuration:
flyctl launch --dockerfile Dockerfile
flyctl deploy
# on first run, overwrite Dockerfile and then roll back needed changes, otherwise errorIn the interactive UI of launch:
- copy configuration: yes
- name 'optimap'
- create a Postgres DB: yes
- connect to
optimap-db: yes - existing database, continue attachment: yes
- (ignore the shown secret for the database connection to connect as the app user, because the migrations need the postgres user)
#fly ips allocate-v4 # paid feature!
fly ips allocate-v6fly ips listConfigure A and AAAA records for @ and www (e.g., with IONOS) at domain registrar using the information of the previous command's output.
Then continue with
flyctl certs create optimap.science
#flyctl certs create www.optimap.scienceCheck IP config:
traceroute optimap.fly.dev
traceroute optimap.scienceThis should all looks good!
Check that SECRET_KEY environment variable is set, otherwise set it to something... secret:
flyctl secrets list
flyctl secrets set SECRET_KEY="..."Configure login email password (other values are set in fly.toml):
flyctl secrets set OPTIMAP_EMAIL_HOST_PASSWORD="..."Configure the superusers' email (users registering with this emailaddress will become Django superusers), seperated with , (comma):
flyctl secrets set OPTIMAP_SUPERUSER_EMAILS="email@server,email@server,..."You can also set values for the secrets in the Fly.io dashboard.
Using the previously configured settings and configurations:
flyctl deployThen open https://optimap.fly.dev/ and https://optimap.science/.
- https://learndjango.com/tutorials/deploy-django-postgresql-flyio
- See ifgi#42 for links and issue description around CSRF
Add to fly.toml:
CSRF_TRUSTED_ORIGINS = "https://optimap.science"Then flyctl deploy.
fly proxy 15432:5432 -a optimap-dbConnect to database locally at port 15432, e.g., with pgAdmin.
flyctl --app optimap consoleInspect the settings, load test data, etc.
python manage.py loaddata fixtures/test_data.json
python manage.py diffsettingsFor example, when you want to manipulate the database without any open connections:
flyctl scale count 0
flyctl statusTo re-enable, set the scale count to 1.
- Database backups, see https://www.joseferben.com/posts/django-on-flyio/
- Health check endpoint, see https://www.joseferben.com/posts/django-on-flyio/