The Neon Data API is designed to be secure by default. It relies on PostgreSQL's native security model, meaning the API does not have its own separate permission system; it acts as a gateway that respects the roles and Row-Level Security (RLS) policies defined in your database.

Securing your data involves two layers:

  1. Role Privileges (GRANT): Determining which tables the API is allowed to access.
  2. Row-Level Security (RLS): Determining which specific rows a user is allowed to see.

API Roles

When the Data API receives an HTTP request, it switches to a specific PostgreSQL role before executing the query. That role is read from the role claim in the request's JWT: the API runs SET ROLE to the role the claim names. If the token has no role claim, the request falls back to the anonymous role. A valid signature on its own does not grant the authenticated role; the token must actually carry "role": "authenticated".

1. Theauthenticatedrole

Used for: Tokens that carry "role": "authenticated".

When a token's role claim is authenticated, the API switches to the authenticated role. This is the primary role for your application users, and it's the role the default GRANTs target. Neon Auth issues this claim automatically; a custom provider must be configured to include it (see Required JWT claims).

  • The JWT token identifies who the user is (via the sub claim).
  • The authenticated role defines what the application is allowed to touch.

2. Theanonymousrole

Used for: Requests whose token carries no role claim, including unauthenticated or guest access.

Anonymous access still uses a JWT, but no user sign-in is required. How you obtain that token depends on your auth setup:

With Managed Better Auth: Set allowAnonymous: true in the client config. The SDK fetches a short-lived anonymous token (GET /token/anonymous) on the first request, caches it, and sends it as Authorization: Bearer <jwt> on every query.

import { createClient } from '@neon/neon-js';

const client = createClient(import.meta.env.VITE_NEON_DATABASE_URL, {
  auth: {
    allowAnonymous: true,
  },
});

// No sign-in needed. The SDK fetches and caches an anonymous JWT automatically.
const { data, error } = await client.from('public_items').select('*');

With a third-party provider: Check whether your provider supports issuing anonymous or guest tokens. If it does, obtain the token using your provider's method and include it in the Authorization: Bearer <token> header on each request.

  • By default, this role has no permissions.
  • You can explicitly GRANT SELECT permissions to this role to expose public data (for example, a product list or public blog posts) without requiring users to log in.
  • Typically, you'd only GRANT SELECT to this role, not write permissions. The syntax follows the same pattern as for the authenticated role. See Configure schema access for an example.

3. Custom roles

authenticated and anonymous are the two roles Neon sets up for you, but the role claim can name any Postgres role. This is the general rule behind both roles above: the API always runs SET ROLE to whatever the claim says. If you issue tokens with a different role (for example, "role": "admin"), the API switches to a role named admin. For this to work, the role must exist in your database, be granted to the authenticator role (the role the Data API connects as), and hold the privileges the request needs:

CREATE ROLE admin NOLOGIN;
GRANT admin TO authenticator;
GRANT USAGE ON SCHEMA public TO admin;
GRANT SELECT ON your_table_name TO admin;

If any of these is missing, the request fails:

  • The role doesn't exist: 22023 role "admin" does not exist
  • The role isn't granted to authenticator: 42501 permission denied to set role "admin"
  • The role lacks the table grant: 42501 permission denied for table ...

The sections below explain how to configure these roles.

Layer 1: Table Privileges

Before RLS can even apply, the database role must have permission to perform the action (SELECT, INSERT, etc.) on the table.

Automatic configuration

When you enable the Data API via the Console, Neon automatically applies default GRANT statements to the authenticated role for the public schema. See the manual configuration section below for the statements that are applied.

Manual configuration

If you skipped the default Data API setup in the Neon Console or you are adding custom roles or working with schemas other than public, you may need to grant permissions explicitly.

The following example SQL commands grant the authenticated role access to all existing and future tables in the public schema. If your tables are in a different schema (for example, sales, analytics etc), update the schema name accordingly. You can also substitute authenticated with a custom role (for example, admin), but you must ensure that the role exists in your database.

Run these commands in the SQL Editor to ensure your API users can access your tables:

-- 1. Grant usage on the schema
GRANT USAGE ON SCHEMA public TO authenticated;

-- 2. Grant access to existing tables
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES
  IN SCHEMA public TO authenticated;

-- 3. Ensure future tables are automatically accessible
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT, UPDATE, INSERT, DELETE ON TABLES TO authenticated;

-- 4. Grant access to sequences (required for auto-incrementing IDs)
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO authenticated;

Permission denied errors?

If you encounter a "permission denied" error immediately after creating a new table, it is likely because the authenticated role hasn't been granted privileges on it. Running the GRANT commands above usually resolves this.

Layer 2: Row-Level Security (RLS)

Granting SELECT access to the authenticated role allows the API to read all rows in the table. To restrict access to specific data (for example, "users can only see their own posts"), you must enable Row-Level Security and create policies.

Understanding RLS states

RLS has three distinct states that affect data visibility:

StateBehavior
RLS disabledAll authenticated users see all rows (no filtering)
RLS enabled, no policiesAll access is blocked (users see nothing)
RLS enabled + policiesRows filtered by policy rules (typically using auth.user_id())

RLS disabled means no filtering

If RLS is disabled on a table, any authenticated user can see all rows in that table. This is different from "filtering without policies"; it means there is no filtering at all.

Checking RLS status in the Console

The Data API page in the Neon Console shows the RLS status of your tables. If a table has RLS disabled, you'll see a warning message like:

"table_name has RLS disabled. All authenticated users can view all rows in this table(s)."

You can click the Enable RLS button to enable Row-Level Security on the table without writing SQL. After enabling RLS, you'll need to create policies to allow access.

Theuser_idcolumn pattern

To filter rows by user, your tables need a column that stores the user's ID. The Data API provides auth.user_id(), a SQL function that extracts the User ID (sub claim) from the current request's JWT. Use this function to:

  1. Set a default value so new rows are automatically associated with the current user
  2. Filter rows in policies so users only see their own data
-- Example table with user_id column
CREATE TABLE posts (
  id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  user_id text DEFAULT (auth.user_id()) NOT NULL,  -- Automatically set to current user
  content text NOT NULL
);

Example workflow

  1. Enable RLS on the table:

    ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

    Once enabled, all access is blocked by default until a policy is created.

  2. Create a policy:

    CREATE POLICY "User owns data" ON posts
      FOR ALL
      TO authenticated
      USING ( select auth.user_id() = user_id )
      WITH CHECK ( select auth.user_id() = user_id );

Now, even though the authenticated role has SELECT permission on the table, the database will only return rows where the user_id column matches the ID in the user's token.