It started with a teammate's request to connect a server to the database, which led me to think about who should be able to access which data. I studied PostgreSQL's permission structure and am sharing my experience designing ByOrbit's data access approach.
It started with a teammate's request to connect a server
Hello. I'm Choi Ji-won, an intern participating in database design for the ByOrbit project.
Our team handles the entire database, from schema design for the ingredient database to migration management. The work I'm introducing here is the permission design that determines “who can access how much of the database”.
The trigger was a teammate's request. To connect the API server, it needed access to the DB, which required authentication credentials. At the time, the server was using the service_role key, and under our team's operating practice, we had to request the key needed for the server connection from the administrator.
Passing the existing key would have resolved the connection itself. But first, there was something I needed to check.
Does this server really need that much permission?
service_role requests can bypass RLS, the row-level security policies. That is useful for the administrative tasks that need it, but if you give the same permission to a server that only needs to handle limited data, its access scope can grow wider than necessary.
I compared this key to a master key. It was like handing someone who needs to enter one room a key that opens every room.
So instead of sharing the existing key as it was, I decided to revisit the approach and grant only the permissions the server needs for its job.
Before RLS, there were permissions to check as well
To divide access scope by role, I first had to understand PostgreSQL's permission structure.
In this work, I organized the general data read and write permissions into three questions.
Check step | Question | Related permission | Schema access: Can the request access objects inside the schema? | Schema USAGE | Table access: What operations can be performed on the table? | SELECT, INSERT, UPDATE, DELETE, etc. | Row access | Which rows can the operation be applied to? | RLS policy
Schema and table access permissions are granted with GRANT, and on tables where RLS applies, the policy restricts which rows are accessible. Setting up RLS does not by itself create schema and table access permissions.
The building analogy made this easy to understand. Schema permission is the permission to enter the building, table permission is the permission to work in a specific room, and RLS is closer to the rules that determine which of the materials in that room you can handle.
Here is a brief example of the read permission.
-- Allow access to objects inside the public schema
GRANT USAGE ON SCHEMA public TO authenticated;
-- Allow select operations on the transports table
GRANT SELECT ON TABLE public.transports TO authenticated;The first statement alone is not enough to query the table. USAGE is the permission to access objects within the schema, and an actual query requires the table's SELECT permission. The statements above are also not a complete setup that includes RLS policies.
Understanding this distinction also made the order for examining permission issues clear. When data does not come back, I should check first whether the request has permission to use the schema and table, rather than only checking the RLS policy.
Even if a policy allows access, you cannot access a table without the permission to query it.
Even using the same table, the work differs
Allowing table access does not mean every account must perform the same operations.
At ByOrbit, we separated the permissions needed by accounts that perform operations work and by intern accounts. In the policies described in the manuscript, we also use separate permissions such as operational permissions like operations.read and operations.update, and permissions like reference.read, reference.create, and reference.update.
Here, RLS takes on the role of checking whether the request has the relevant permission. For example, the policy calls authorize('reference.read') to verify whether the requesting account has that permission. Note that authorize() is not a command provided by default in PostgreSQL; it is a function we define to check roles and permissions.
While organizing this, I felt I needed to use the expression “can access” more concretely.
The permissions needed differed depending on whether the account could only read, could register new data, or could modify or delete existing data. It was more important to first define the tasks the person will actually perform than to simply allow or block a user.
Even the same “role” term needed to be distinguished
Another point I found confusing was the term role.
The policy says TO authenticated, while within the service, other role names such as user, intern, and api were in use. At first they all seemed to be the same kind of role, but I needed to distinguish between two things.
First, authenticated is an actual PostgreSQL role. In the typical Data API authentication flow of the database, requests from logged-in users are processed under this role.
On the other hand, user, intern, and api in the structure I examined this time are roles used to distinguish work within the service. This information can be included in an authentication token (JWT) item such as user_role, and it is used to evaluate permissions in RLS policies.
For example, for an API-only account, database requests use the authenticated role, while the token carries api as the user_role value.
Separating these made the responsibility of each setting clear.
GRANT grants permissions to actual database roles, while RLS uses the business role and permission information included in the request to decide access conditions. Adding a business role to the JWT and creating a new PostgreSQL role are different tasks.
Only the permissions needed, even for the API server
After organizing the concepts, I redesigned the API server's access approach. The direction was to stop relying on the service_role key for general data reads and writes, and instead go through authentication and permission checks suited to the server's work.
The flow outlined in the original diagram is to authenticate with an API-only account, obtain a token that carries the business role, and send requests with it. On the database side, the setup checks both the access permissions of the PostgreSQL role processing the request and the RLS policies.
What mattered was not deciding the account name would be api. It was deciding which tables that account should use, whether it only needs to read or also needs to create and update, and which data it should be able to reach.
The goal was to specify the scope within which the server can perform its job, instead of continuing to pass along the strong permissions received from the administrator.
Of course, separating roles does not make credential management unnecessary. The dedicated account and tokens also need to be protected. I understood that limiting permissions is not about eliminating security concerns, but about designing to reduce the scope of impact if credentials are exposed or misused.
What I learned as an intern: looking at what comes after the connection
This work started with a request to connect a server. But what I actually thought through was less the connection method and more what to allow the connected server to do.
Looking only at the initial request, it could have ended with simply passing along the credentials. Revisiting the permission scope in the process let me organize how schema access, table operation permissions, and RLS policies work together.
What I remember most is that broadening permissions all at once may make the immediate task easier, but makes it harder to explain why those permissions are needed. Conversely, starting from the tasks actually required made both the permissions to configure and the conditions to verify gradually clearer.
Next, I plan to extend this structure to ByOrbit's buyer accounts. The next task is to design permissions by account type so that buyers can access only the pages and data allowed to them.
At that stage, beyond distinguishing the buyer role, I also need to examine the conditions that identify whose data it is even among accounts with the same buyer role. RLS can be used to express these per-user row access conditions.
There is still a lot to learn, but since this work, I have one more question I check when looking at a database.
Rather than stopping at “Does the data come out correctly?”, I now also ask “Should this data come out for this request?”
I intend to keep asking this question in future designs.
