Database Setup
The data layer stores entities in PostgreSQL. This page covers connecting to the database, encrypting the connection, the schemas Hologram keeps its tables in, and sharing the database with an Ecto repo.
PostgreSQL Requirements
The data layer needs PostgreSQL 15 or newer. Postgrex, the driver Hologram connects with, comes with Hologram, so there's no dependency to add.
The database has to have the UTF8 encoding, the usual one for PostgreSQL. Hologram writes text as UTF-8, and in a database with another encoding the server misreads it, so things like text length, letter case and pattern matching come out wrong. Hologram refuses to set up its schemas in such a database. You choose the encoding when you create the database and can't change it later.
Database Configuration
Connection settings go under config :hologram, :database. Hologram reads them when the app boots, not while it compiles the bundles, so unlike most settings on the Configuration page they work from config/runtime.exs, the usual place for production credentials.
config :hologram, :database,
url: System.fetch_env!("DATABASE_URL"),
pool_size: 20
The connection can also be given part by part:
config :hologram, :database,
host: "db.internal",
port: 5432,
database: "my_app",
user: "my_app",
password: System.fetch_env!("DATABASE_PASSWORD")
The keys:
url- the connection as one URL, in PostgreSQL's connection URI format, likepostgres://user:password@host:5432/database. Keys below can still be set next to it, for anything the URL leaves out, like a password you'd rather keep out of it. A key the URL already covers is ignored. Characters with a special meaning in a URL, like@,:,/,?,#or%, are written percent-encoded in the user, password or database name, so a passwordp@ss/wordgoes in asp%40ss%2Fword.host,port,database,userandpassword- the connection.portdefaults to5432.pool_size- how many connections the app's pool keeps open,10by default. Hologram opens a few more for sync and jobs. Deploying lists them all, so you can check them against PostgreSQL's connection limit.ssl- whether the connection is encrypted with TLS,falseby default. See Encrypted Connections below.
In dev and test every key has a default: localhost, the user postgres with the password postgres, and a database named after the app and the environment, like my_app_dev. With a local PostgreSQL that has the default superuser, you don't need any configuration. To change one key, set just that one:
config :hologram, :database, port: 5433
Hologram doesn't create the database when the app boots. Run mix holo.db.reset once to create the dev database, with the UTF8 encoding, and build its schema. If the database is missing, the boot fails with an error that names this task. Migrations covers what else the task does.
In every other environment there are no defaults for host, database, user and password, and the app refuses to boot when one of them is missing.
As with the rest of Hologram, nothing starts in dev and test unless HOLOGRAM_START=1 is set, which mix holo does for you. So compiling the app or running a Mix task doesn't connect to the database.
Encrypted Connections
Set ssl: true to encrypt the connection and check the server's certificate against the system's CAs, host name included:
config :hologram, :database,
url: System.fetch_env!("DATABASE_URL"),
ssl: true
For more control, give a list instead:
config :hologram, :database,
url: System.fetch_env!("DATABASE_URL"),
ssl: [mode: :verify_full, ca_cert_path: "/etc/ssl/db-ca.pem"]
The entries:
mode- how much of the server's certificate is checked.:verify_full, the default, checks that a trusted CA signed it and that it was issued for the host you connect to.:verify_cachecks the CA but not the host name, so it needsca_cert_path.:requireencrypts without checking the certificate, so it hides the traffic from anyone listening in, but not from someone posing as the server.ca_cert_path- the file of the CA to trust instead of the system's CAs, like the one your database provider gives you. It needs:verify_caor:verify_full, since:requirechecks no certificate.:verify_caalways needs it, since a public CA signs certificates for anybody's host, so trusting the system's CAs takes the host name check of:verify_full. On a machine with no system CAs, like some slim container images,:verify_fullneeds it too, and the app refuses to boot without it.cert_pathandkey_path- the files of a client certificate and its key, for a server that asks the client for one. Set them together.
:verify_ca is for a certificate from your own CA that doesn't name the host, like a self-signed one. Under it, Erlang's TLS library no longer checks that the CAs' certificates are signed with an algorithm the connection supports, and when the host name doesn't match, it skips that check for the server's certificate too. It still verifies the signatures themselves. Use :verify_full when the certificate names the host.
The URL can set TLS too, with PostgreSQL's own query parameters. Each one replaces only its own ssl entry, the way the URL's host and port replace their keys, and the other entries stay:
sslmode-require,verify-caorverify-fullsetsmode, anddisableturns TLS off, whateversslsays.allowandpreferaren't supported, since Postgrex can't fall back between an encrypted and a plain connection. Userequireordisableinstead.sslrootcert- setsca_cert_pathand, like that entry, needs a mode that checks the certificate. Unlike in PostgreSQL's own client, it doesn't implyverify-ca, andverify-caneeds it, set to a file.sslrootcert=systemmeans the system's CAs and needsverify-full.sslcertandsslkey- setcert_pathandkey_path.channel_binding- Hologram acceptsdisableandprefer, which change nothing.requireisn't supported, since Postgrex can't bind the channel, so remove it from a connection string that has it.
sslrootcert, sslcert or sslkey on a connection that ends up unencrypted is an error, since it would be ignored. Any other parameter is an error that names it, ssl and pool_size included. Set those two as keys next to url. No error message shows the URL, since it carries the password.
A TLS problem fails the boot instead of waiting for the database. That covers a server certificate no trusted CA signed or that doesn't match the host, a client certificate the server rejects, a certificate file that's missing, and a server that doesn't offer TLS. Hologram still waits for a server that's down or restarting, encrypted or not.
Hologram's Schemas
Hologram keeps everything it owns in two PostgreSQL schemas:
hologram_data- the entity tables, which Hologram creates and changes to match your entity types.hologram_system- Hologram's own bookkeeping, like the oplog and the migration history.
Not with SQL, an Ecto repo or another tool. Browsers only learn about changes that went through the data layer, so an entity changed in place keeps its old values in every browser that holds it. A direct write also skips validation, access rules and the system fields Hologram keeps on each entity. Read entities with queries, and create, update and delete them with DB.create, DB.update and DB.delete. For the rare read a query can't express, DB.sql runs raw SQL, and it refuses to write. Leave the schema itself to Hologram.
Sharing a Database with Ecto
Ecto isn't required, but an app that already has an Ecto repo can keep it and point it at the same database. Ecto's tables stay in their own schema, usually public, and Hologram keeps to hologram_data and hologram_system, so it never touches them. The repo should stay out of Hologram's tables in turn, as the box above explains. mix holo.db.reset drops only Hologram's two schemas, so a reset keeps the repo's tables.
The two use separate connection pools, so they don't share transactions: a DB.transaction doesn't cover an Ecto write, and an Ecto transaction doesn't cover a data layer write. Hologram guards against mixing them up. An Ecto query run inside a data layer transaction fails that transaction, rather than committing on its own while the rest rolls back. This applies to Ash with AshPostgres too, which runs on an Ecto repo.