Skip to main content

Database Roles and TLS

By default an app connected to an add-on signs in as the add-on's owner, which can do anything in the database. A database role is a separate login with only the rights your app needs, so a bug or a leaked password in the app cannot drop a table or read another database. PostgreSQL add-ons can also encrypt their connections with TLS, an option you turn on (it is off by default); apps then verify the server's certificate.

Database roles​

Roles are available on PostgreSQL, DuckDB, MySQL and MongoDB add-ons. The add-on must be running to create, change or delete a role. Managing roles takes the same rights as changing the add-on (its owner and editors).

Presets​

Every role has one of two presets. There is no other preset, and no role is ever a superuser.

PresetAllowsDoes not allow
Read-onlyReading every table in the add-on's database (PostgreSQL and DuckDB: every schema, and every session is read-only; MySQL: SELECT; MongoDB: the built-in read role)Inserting, updating or deleting anything; any schema change
Read-writeReading, inserting, updating and deleting rows (MongoDB: find, insert, update and remove documents)Creating, altering or dropping tables, indexes or schemas; TRUNCATE; creating roles or databases; superuser rights

Neither preset can change the schema. Schema changes (migrations) run as the owner: see The owner connection for migrations.

Tables the owner creates later are covered automatically on PostgreSQL and DuckDB. When the owner creates a new schema, apply the role's privileges again so the role can use it.

Create a role​

Give the role a name and pick a preset. The name is lowercase letters, digits and underscores, starts with a letter and is 3 to 63 characters long; it cannot start with pg_ or clone_, be the add-on's owner user, or be a reserved name such as postgres, root or admin.

The role's password is generated for you and shown once, when the role is created. Copy it then if you need it outside an app: it is stored encrypted and is never shown again. Apps bound to the role receive it automatically.

Rotate a password​

Rotating gives the role a new password, again shown once. Running apps bound to the role restart to pick it up, so expect a short restart for each of them.

Apply privileges again​

Re-applies the role's preset, so it covers schemas created since the role was made.

Delete a role​

Deleting a role closes its open sessions and removes it from the database. A role an app is bound to cannot be deleted: the refusal names the apps. Bind them to another role (or to the owner connection) first.

Bind an app to a role​

When you connect an add-on to an app, choose the role the app signs in as. You can change the role of an app that is already connected; choosing no role puts the app back on the owner connection.

For a bound app, the add-on's entry in STRONGLY_SERVICES carries the role's credentials in connection.connection_string, connection.uri and auth.credentials, and names the role (this add-on has TLS off, the default; see TLS for what changes when it is on):

{
"id": "addon-abc123defg",
"name": "orders-db",
"type": "postgres",
"connection": {
"connection_string": "postgresql://orders_app:<password>@<internal-host>:5432/defaultdb",
"uri": "postgresql://orders_app:<password>@<internal-host>:5432/defaultdb",
"host": "<internal-host>",
"port": 5432,
"database": "defaultdb"
},
"auth": {
"method": "username_password",
"credentials": { "username": "orders_app", "password": "<password>" }
},
"role": { "id": "<role id>", "name": "orders_app", "preset": "read-write" }
}

The owner connection for migrations​

Turn on the owner connection for an app that runs database migrations. Its entry then also carries admin_connection with the owner's connection_string, uri, username and password.

The recommended pattern:

  • Bind the app to a read-write role and serve all traffic with connection.connection_string.
  • Run migrations, and only migrations, with admin_connection.
  • Leave the owner connection off for every app that does not run migrations.
import json
import os
import psycopg

services = json.loads(os.environ["STRONGLY_SERVICES"])
pg = next(a for a in services["services"]["addons"]["postgres"] if a["name"] == "orders-db")

# Migrations: the owner connection (present only when it is turned on for this app)
with psycopg.connect(pg["admin_connection"]["connection_string"]) as admin:
admin.execute("CREATE TABLE IF NOT EXISTS orders (id serial PRIMARY KEY, total numeric)")

# Traffic: the read-write role
conn = psycopg.connect(pg["connection"]["connection_string"])

TLS (PostgreSQL)​

TLS is an option, off by default: a new PostgreSQL add-on accepts plain connections, and the platform never turns TLS on by itself (not at creation, not at a restart). Its owner or an editor turns it on in the Encryption in transit (TLS) card on the add-on details page, with the add-on running.

Serve TLS​

Serve TLS makes the add-on serve TLS with its own certificate, issued by the platform's certificate authority and renewed automatically before it expires. Plain connections are still accepted until you also require TLS. Turning it on or off restarts the add-on and the running apps that use it, so they reconnect with the new connection details.

Require TLS​

Require TLS is a second choice, also off by default, available once TLS is on. It makes the add-on refuse unencrypted connections; with it off, encrypted and unencrypted connections are both accepted. Turning TLS off stops requiring it. Changing it restarts the add-on and the running apps that use it.

Connect with verify-full​

Once TLS is on, the entry's connection.ssl is { "mode": "verify-full", "root_cert_path": "/etc/strongly/addon-ca/ca.crt" } and the connection string already ends with sslmode=verify-full&sslrootcert=/etc/strongly/addon-ca/ca.crt. verify-full checks both that the certificate was issued by the platform's certificate authority and that it names the host you connect to.

The certificate authority file /etc/strongly/addon-ca/ca.crt is mounted read-only in every app, workspace and workflow that receives STRONGLY_SERVICES. Always connect from the entry, never from a hard-coded host or path.

import json
import os
import psycopg

services = json.loads(os.environ["STRONGLY_SERVICES"])
pg = next(a for a in services["services"]["addons"]["postgres"] if a["name"] == "orders-db")

# The connection string already carries sslmode=verify-full and sslrootcert
conn = psycopg.connect(pg["connection"]["connection_string"])

# Or with separate parameters
c = pg["connection"]
conn = psycopg.connect(
host=c["host"],
port=c["port"],
dbname=c["database"],
user=pg["auth"]["credentials"]["username"],
password=pg["auth"]["credentials"]["password"],
sslmode=c["ssl"]["mode"],
sslrootcert=c["ssl"]["root_cert_path"],
)

Use the API​

The same actions are available through the REST API:

MethodPathWhat it does
GET/api/v1/addons/:addonId/rolesList roles with the apps bound to each (no passwords)
POST/api/v1/addons/:addonId/rolesCreate a role (name, preset); the password is in the answer once
GET/api/v1/addons/:addonId/roles/:idOne role
DELETE/api/v1/addons/:addonId/roles/:idDelete a role
POST/api/v1/addons/:addonId/roles/:id/rotate-passwordNew password, shown once
POST/api/v1/addons/:addonId/roles/:id/apply-privilegesApply the preset's privileges again
PATCH/api/v1/addons/:idtlsEnabled turns TLS on or off; requireTls (only with TLS on) turns Require TLS on or off
POST/api/v1/addons/:id/connectionsConnect an app (appId, optional roleId, adminConnection)
PATCH/api/v1/addons/:addonId/connections/:idChange a connected app's roleId and adminConnection