

# PostgreSQL differences

The Amplify backend database runs PostgreSQL 17, in deployed environments and on the local backend. You can change timeouts for a session, a transaction or a function. Superuser settings, roles and other features work differently from a PostgreSQL server that you run. To turn on an extension, see [Extensions](backend-extensions.md). To connect, see [Connect to your database](backend-database-connect.md).

## Change a setting for a session
<a name="change-a-setting-for-a-session"></a>

In a `psql`, `aws amplifygen3 db query` or server-code session, you can change `statement_timeout`, `lock_timeout` and `idle_in_transaction_session_timeout` for the session or for one transaction. A data API request can’t run `set`. To change a timeout for the current session, use `set`. To change it for one transaction, use `set local` inside the transaction:

```sql
set statement_timeout = '5min';

begin;
set local lock_timeout = '2s';
-- ...
commit;
```

To give a function its own timeout, add a `set` clause to its definition. In a `psql` or `aws amplifygen3 db query` session, the clause overrides the session’s value, including in a `security definer` function. PostgreSQL drops every `set` clause that a `create or replace function` statement leaves out, so repeat the clause in each new definition:

```sql
create or replace function public.monthly_totals()
returns setof public.totals
language sql
set statement_timeout = '30s'
as $$ select * from public.totals $$;
```

A `set` clause in a function doesn’t extend a data API call. The call keeps its role’s cap: 3 seconds for `anon`, 8 seconds for `authenticated`, and 120 seconds for `service_role`. For work that takes longer, use `aws amplifygen3 db query` or a migration.

## Statement timeouts
<a name="backend-postgres-timeouts"></a>

A session starts with the following timeouts, based on how it connects.


| Session |  `statement_timeout`  |  `idle_in_transaction_session_timeout`  | 
| --- | --- | --- | 
| Data API as `anon`  | 3 seconds | 60 seconds | 
| Data API as `authenticated`  | 8 seconds | 60 seconds | 
| Data API as `service_role`  | 120 seconds | 60 seconds | 
|  `psql` and the `aws amplifygen3 db` commands | 30 minutes | 10 minutes | 
| Migrations that a deployment applies | None | None | 

A data API query past its timeout fails with `500` and the code `57014`. A deployed backend returns `504` for a request that gets no response within 60 seconds. A `service_role` query that runs longer than 60 seconds doesn’t return its result, even though its timeout is 120 seconds. To run a query that takes longer, use `aws amplifygen3 db query` or a migration.

PostgreSQL ends a transaction that stays idle for longer than `idle_in_transaction_session_timeout`. Commit each transaction before your code calls another service.

## Text sort order
<a name="backend-postgres-sort"></a>

A deployed database sorts text with a language collation. The local database sorts text by code point (`C.UTF-8`), so `order by name` can return rows in a different order on your computer. To get the same order in both, such as for a test or for keyset pagination, name the collation in the query:

```sql
select name from public.projects order by name collate "C";
```

## What differs from a PostgreSQL server that you run
<a name="backend-postgres-differs"></a>

These features work differently in the database:
+  **Superuser and server settings** – Database logins aren’t superusers. `alter system` fails with `permission denied to set parameter`, and so does changing a superuser-only setting, such as `deadlock_timeout` or `log_lock_waits`. To change a timeout, see [Change a setting for a session](#change-a-setting-for-a-session).
+  **Roles** – `create role` fails with `42501`. To control who reads and changes rows, grant access to your tables to `anon` and `authenticated`, and write row-level security (RLS) policies. For more information, see [Secure your data](backend-rls.md).
+  **Schemas of your own** – A migration can create a schema of your own, such as `private` for a `security definer` helper that the data API doesn’t serve. Amplify reserves the names `amplify`, `auth`, `extensions`, `storage`, `realtime` and a few others, and a migration that creates a schema with a reserved name fails.
+  **The `auth` schema** – Your tables can reference `auth."user"(id)`. A migration that creates or changes an object in `auth` fails.
+  **Temporary files** – Data API sessions and `aws amplifygen3 db` sessions have a `temp_file_limit` of 1 GB that a session can’t change. A query whose temporary files grow past the limit fails with `53400`. Migrations have no such limit.
+  **Scheduled jobs** – To run a job on a schedule, run it outside the backend and have it call the data API.

For connection limits and other quotas, see [Quotas](quotas-chapter.md).