Scrivio Interview library
CodingFoundations8 min read · updated 12 November 2026

A Spring Boot service connects to PostgreSQL as the database owner. Write the SQL to create a separate login role for the application that can only read and write the tables in schema app, and a migration role that owns the objects.

We split the one powerful owner account into three roles: app_owner, a role that cannot log in and owns every table in schema app; app_migrator, a login that is a member of app_owner and runs Flyway or Liquibase with SET ROLE app_owner; and app_rw, the login the Spring Boot service uses, which only has USAGE on the schema and SELECT, INSERT, UPDATE and DELETE on its tables and sequences. ALTER DEFAULT PRIVILEGES makes sure tables created later are granted to app_rw automatically.

Why this question is asked

Many Spring Boot services start life connecting to PostgreSQL as the user that created the database, because it is the quickest way to make the first migration run. That user can drop tables, change grants and read every schema, so a single SQL injection bug or a leaked password gives an attacker everything. Interviewers ask this question because writing the grants correctly shows that we understand how PostgreSQL ownership and privileges really work, not only that least privilege is a good idea. In this answer, we will recap roles and ownership, create the owner, migrator and application roles step by step, cover tables that do not exist yet, lock down the public schema, and wire the two logins into Spring Boot.

1A Quick Recap of Roles, Ownership and Privileges

In PostgreSQL there is only one kind of account, the role. A role with the LOGIN attribute behaves like a user, and a role without it behaves like a group that other roles can be members of. When a role is a member of another role, it can use that role's privileges, and with SET ROLE it can act as that role completely.

Every table, sequence and view has an owner, which is the role that created it. The owner can do anything to the object, including ALTER and DROP, and nobody can take those rights away from the owner with REVOKE. Other roles get access only through GRANT. This is the key fact for our design: whoever owns the tables can change the schema, so the application must not be the owner.

A schema is a namespace inside a database, like a folder for tables. To touch any object in schema app, a role needs USAGE on the schema itself and then a privilege on the object, such as SELECT on a table. Missing either one gives a permission error.

2Creating the Owner and the Migration Role

We start with a role that owns everything but can never log in. Because nobody can connect as it directly, its password can never leak. The migration tool logs in as a separate role that is a member of it.

SQL
-- run once as a superuser or the current database owner
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_migrator LOGIN PASSWORD 'from-the-secret-store' IN ROLE app_owner;
CREATE SCHEMA app AUTHORIZATION app_owner; -- the schema itself belongs to app_owner
-- what a migration does first, so new objects belong to app_owner
SET ROLE app_owner;
CREATE TABLE app.orders (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, total numeric(12,2));
SELECT tableowner FROM pg_tables WHERE tablename = 'orders'; -- app_owner

Notice the SET ROLE line. A common belief is that membership alone makes app_owner the owner of what app_migrator creates, but that is not how PostgreSQL works: a new object belongs to the current role at the moment it is created. If the migration runs without SET ROLE, the tables end up owned by app_migrator, and the default privileges we set up in a moment will not fire for them. So the migration session must switch to app_owner first. Flyway and Liquibase both let us run a statement when they open their connection, which is the cleanest place for it.

We keep a separate migrator login instead of logging in as app_owner so we can rotate or disable it on its own and see in the logs which login ran a migration.

3Creating the Application Role

Now the role the running service uses. It gets exactly what a normal request needs and nothing more.

SQL
CREATE ROLE app_rw LOGIN PASSWORD 'from-the-secret-store';
GRANT CONNECT ON DATABASE shop TO app_rw, app_migrator;
GRANT USAGE ON SCHEMA app TO app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app TO app_rw;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA app TO app_rw; -- nextval() for serial columns
-- as app_rw:
INSERT INTO app.orders (total) VALUES (19.90); -- INSERT 0 1
DROP TABLE app.orders; -- ERROR: must be owner of table orders
CREATE TABLE app.tmp (x int); -- ERROR: permission denied for schema app
The three roles and what each can do
RoleLogs inOwns objectsCan doUsed by
app_ownernoyes, all of schema appeverything, including DDLnobody directly
app_migratoryesno (acts via SET ROLE)DDL through app_ownerFlyway or Liquibase
app_rwyesnoSELECT, INSERT, UPDATE, DELETEthe Spring Boot service
Only the migration path can change the schema; the service can only read and write rows.

Each grant has a reason. USAGE on the schema lets app_rw see the objects in it. The four table privileges cover what JPA or JdbcTemplate does at runtime. USAGE on sequences matters for columns declared as serial or with a sequence default, because the INSERT calls nextval() and that needs a privilege on the sequence; identity columns like the one above do not need it, but older tables often use serial. TRUNCATE, REFERENCES and TRIGGER are deliberately left out.

But there is a catch: GRANT ... ON ALL TABLES only covers the tables that exist right now. The next migration that adds a table would leave app_rw without access, and the service would fail with a permission error after the deploy.

4Covering Tables That Do Not Exist Yet

Default privileges solve this. They tell PostgreSQL which grants to add automatically when a particular role creates a new object in a particular schema.

SQL
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_rw;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app
GRANT USAGE ON SEQUENCES TO app_rw;
SET ROLE app_owner;
CREATE TABLE app.payments (id bigserial PRIMARY KEY);
SELECT has_table_privilege('app_rw', 'app.payments', 'INSERT'); -- true

The words FOR ROLE app_owner are important. Default privileges belong to the role that creates the object, so this rule fires only for objects created while the current role is app_owner. This is the second reason the migration must run SET ROLE app_owner: without it, the creator is app_migrator, this rule does not apply, and the grant is silently missing.

5Locking Down the Public Schema and DDL Rights

Every database also has a schema called public. Before PostgreSQL 15, every role could create tables there by default, which is an easy way for an attacker with the application password to create objects, including functions that shadow built-in ones. Since PostgreSQL 15 that default is gone for new databases, but databases that were upgraded from older versions keep the old grant, so we revoke it explicitly.

Java
REVOKE CREATE ON SCHEMA public FROM PUBLIC; -- default in new databases since PostgreSQL 15
REVOKE ALL ON DATABASE shop FROM PUBLIC; -- only roles we grant CONNECT to may connect
ALTER ROLE app_rw SET search_path = app; -- unqualified names resolve to schema app

Notice that app_rw never received CREATE on any schema and is not a member of app_owner. It cannot run ALTER TABLE, CREATE INDEX or DROP, so even a SQL injection bug in the service is limited to the rows the service could already change. Setting search_path for the role also stops anyone from placing objects earlier in the search path to hijack a query.

6Wiring the Two Logins Into Spring Boot

The service and the migration tool now use different credentials, and Spring Boot supports that directly: Flyway and Liquibase can be given their own user, separate from the main DataSource.

YAML
# application.yml (passwords come from the environment or a secret manager)
spring:
datasource:
url: jdbc:postgresql://db:5432/shop
username: app_rw
password: ${APP_RW_PASSWORD}
flyway:
user: app_migrator
password: ${APP_MIGRATOR_PASSWORD}
schemas: app
init-sqls: SET ROLE app_owner # every Flyway connection acts as the owner

So the same application starts, runs its migrations as app_migrator acting as app_owner, and then serves traffic as app_rw. Running migrations as a separate job before the deploy is even better, because then the migrator password never reaches the running pods. Check the property names for your Boot version.

7Follow-up questions

How would you give a reporting tool read-only access?

We create a group role app_ro without LOGIN, grant it USAGE on schema app and SELECT on all tables, and add ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app GRANT SELECT ON TABLES TO app_ro so new tables follow. The reporting tool then gets its own login, for example report_user, that is a member of app_ro, ideally pointed at a read replica so heavy reports do not slow the primary. If the tool must never see personal data, we grant SELECT on masked views instead of the base tables. The predefined role pg_read_all_data is tempting but it covers every schema in the database, which is broader than a reporting tool needs.

What if the existing tables are already owned by the old database owner?

We move them with ALTER TABLE ... OWNER TO app_owner for each object, or REASSIGN OWNED BY old_owner TO app_owner when the old role owns only application objects. Run it in a maintenance window or a migration, then re-run the grants, because the grants of the old owner are not a substitute for the new ones.

8Conclusion

In this answer, we saw that least privilege in PostgreSQL rests on the difference between owning an object and being granted access to it. We created a non-login app_owner that owns schema app, an app_migrator login that runs migrations with SET ROLE app_owner so ownership and default privileges come out right, and an app_rw login with only USAGE and row-level DML grants. Default privileges cover future tables, revoking CREATE on public closes an old loophole, and Spring Boot runs Flyway with the migrator while the service runs with app_rw.

What to say in the interviewAbout 80 seconds aloud

Open with

I would split the database owner into three roles. First app_owner, created with NOLOGIN, which owns schema app and every object in it, so its password cannot leak because it has none.

Then, if they want more

Second app_migrator, a login role that is a member of app_owner; Flyway or Liquibase logs in with it and runs SET ROLE app_owner first, because in PostgreSQL an object belongs to the current role that creates it, not to the group. Third app_rw, the login the Spring Boot service uses.

It gets CONNECT on the database, USAGE on schema app, SELECT, INSERT, UPDATE and DELETE on all tables, and USAGE on the sequences so serial columns work. It gets no CREATE anywhere and no membership in the owner role, so it cannot alter or drop anything.

Because GRANT ON ALL TABLES only covers existing tables, I add ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA app so new tables are granted to app_rw automatically. I also revoke CREATE on the public schema from PUBLIC, which is the default only since PostgreSQL 15 and is often still present in upgraded databases.

In Spring Boot, spring.datasource uses app_rw, and spring.flyway.user uses app_migrator, or better, migrations run as a separate job so the migrator password never lives in the service at all.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring Boot service connects to PostgreSQL as the database owner. Write the SQL to...": https://scrivio-interview-library.netlify.app/databases/database-security/596aeae4ac/