ETLMCPData Conveyance Works
Log in Start free
Guides

A read-only PostgreSQL role for dashboards that sees new etlmcp tables

Give your BI tool a read-only PostgreSQL login that can also read the tables etlmcp creates later, with GRANT plus ALTER DEFAULT PRIVILEGES.

A dashboard login that was granted SELECT on the tables in a schema cannot read a table etlmcp creates afterwards. To fix that for good, run two statements: a GRANT for the tables that already exist, and an ALTER DEFAULT PRIVILEGES FOR ROLE naming the user etlmcp logs in as, which covers every table that user creates in the schema from then on.

Why a new etlmcp table is invisible to your dashboard

etlmcp connects with the user you typed in the destination settings and nothing else. On the first run that loads rows, if the table you named is not there, etlmcp creates it itself with CREATE TABLE, as that user. The Test button also creates a missing table, but it rolls everything back, so the table really appears with the first load. See the PostgreSQL destination setup for the table's columns.

PostgreSQL makes the role that ran the creation statement the owner of a new table, and at first only the owner (or a superuser) can do anything with it. No privileges on tables go to PUBLIC by default. A new table gets its privileges at creation time, from the default privileges of the role creating it, not from grants you ran earlier. So GRANT SELECT ON ALL TABLES IN SCHEMA acts on the tables in the schema when you run it, and a table created the next day starts without your dashboard role.

With etlmcp, a new table appears whenever you add a PostgreSQL destination with a table name that does not exist yet, or change the table name of an existing one. Pipelines that write to a table already there do not create a new one.

The SQL

Replace marketing with your schema, etlmcp_loader with the user etlmcp logs in as, and dashboards with the name you want for the BI login.

-- 1. A login for the BI tool
CREATE ROLE dashboards WITH LOGIN PASSWORD 'choose-a-password';

-- 2. Let it look up objects in the schema
GRANT USAGE ON SCHEMA marketing TO dashboards;

-- 3. Tables that exist today
GRANT SELECT ON ALL TABLES IN SCHEMA marketing TO dashboards;

-- 4. Tables the etlmcp user creates later in this schema
ALTER DEFAULT PRIVILEGES FOR ROLE etlmcp_loader IN SCHEMA marketing
  GRANT SELECT ON TABLES TO dashboards;

What each part does:

  • USAGE on a schema is what lets a role find the objects inside it. Without it, SELECT on a table is not enough.
  • In GRANT, ALL TABLES also covers views and foreign tables in the schema.
  • ALTER DEFAULT PRIVILEGES never changes objects that already exist, which is why step 3 is still needed. It only applies to objects later created by the role named in FOR ROLE; leave that clause out and it means the role running the statement. Tables created by any other role, such as views you build yourself as an admin, are not covered: add an ALTER DEFAULT PRIVILEGES FOR ROLE for that role too, or grant on them one by one.
  • The schema in IN SCHEMA must already exist. Leave IN SCHEMA out to cover tables the etlmcp user creates in any schema of the database.
  • You may change the default privileges of your own role or of a role you are a member of. The plainest way is to run step 4 while logged in as the etlmcp user.

If the BI login must read everything in the database, PostgreSQL's predefined role pg_read_all_data acts as if it had SELECT on every table and USAGE on every schema. It is far broader than one schema, so prefer the grants above when the database holds anything else.

Check the result

In psql, \ddp lists the default privileges that differ from the built-in ones, one line per role and schema. You should see a line for etlmcp_loader in marketing that gives dashboards the r (SELECT) right on tables. \dp marketing.* lists the privileges on the tables that exist now. After etlmcp's next new table appears, \dp should show dashboards on it without any further grant.

Keep the table owned by the etlmcp user

Leave ownership of etlmcp's tables with its user. When a platform sends a new extra. field, etlmcp adds a column with ALTER TABLE; if its login may not alter the table, for example because you gave the table to another owner, the run stops with the missing-columns error described in PostgreSQL destination errors. Do not create the table in advance as your admin user either: etlmcp refuses a table that lacks its _etlmcp_pipeline_id column and asks for a new table name.

The read-only role does not affect how each run replaces its date range. What the loading user itself needs, such as the right to create tables in the schema, is a separate topic.

Related

Sources

  1. ALTER DEFAULT PRIVILEGES — www.postgresql.org, checked
  2. GRANT — www.postgresql.org, checked
  3. 5.9. Privileges — www.postgresql.org, checked
  4. CREATE ROLE — www.postgresql.org, checked
  5. psql — www.postgresql.org, checked
  6. 21.5. Predefined Roles — www.postgresql.org, checked