Skip to main content

Web and Database Application

In this example, we'll walk through an example of enabling preview environments for a Python (Django) application that connects to a Postgres database. The sample repository can be found here.

porter.yaml File Walkthrough

The porter.yaml file to define both a Postgres database and a web application is as follows:

version: v1
- name: postgres
name: postgresql
postgresqlPassword: testing
- name: web
- postgres
name: web
method: pack
builder: heroku/buildpacks:20
command: web
port: 8000
POSTGRES_USERNAME: "{ .postgres.postgresqlUsername }"
POSTGRES_PASSWORD: "{ .postgres.postgresqlPassword }"
  • postgres.source: by setting the field to postgresql, Porter will look for a supported addon called postgresql.
  • postgres.config: the postgresql addon accepts the postgresqlPassword field to set the default Postgres password.
  • web.source: by setting the field to web, Porter will deploy this resource as a web application.
  • web.depends_on: by setting the depends_on field to include postgres, Porter will wait for the postgres resource to be created before deploying the web application.
  • specifying the field as pack instructs Porter to build this application using cloud-native buildpacks, and setting the builder to heroku/buildpacks:20 means that this buildpack will be used.
  • web.config.values: here, we set common configuration settings, such as the start command, the port that the application runs on, and environment variables for the application. Also, note that configuration references an output variable from the postgres resource.