PostgreSQL (RDMS)#

Dokku has official support for postgres, a good and mature open source relational database. The dokku plugin for this is well documented. It can be used to launch & manage databases and link them to apps.

Creating a database for your app#

# create the database
dokku postgres:create <db-name>

# connect to the database
dokku postgres:connect <db-name>

Provisioning the database from an existing database#

# dump data from local database
#  (NOTE: pg_dump likely requires additional parameters like the hostname, username & password to local db)
pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w db_name > db.dump

# send dump to dokku database
dokku postgres:import <db-name> < db.dump

Linking the database to your app#

# assuming the db and app are created
dokku postgres:link <db-name> <app-name>

This will set an environment variable: DATABASE_URL which contains a connection URI of the form postgres://<username>:<auto-generated-password>@<service-name>:5432/<db-name>.

Importantly, your app should receive this variable and use it to configure the database. For example, in python:

import os
import psycopg2

DATABASE_URL = os.environ.get('DATABASE_URL')
assert DATABASE_URL is not None, 'Missing DATABASE_URL environment variable to connect to the database'

# connect to db
conn = psycopg2.connect(DATABASE_URL)

# ... use conn in your app to build queries ...

conn.close()

Exporting from dokku postgres#

# data in a dokku database can also be exported similarly to pg_dump
dokku postgres:export <db-name> > db.dump