

# Local database

The local backend runs a PostgreSQL database on your computer for your Amplify backend. You change its schema with migrations, run SQL against it, load seed data, reset it and generate TypeScript types from it with the `aws amplifygen3 local` commands of the AWS Amplify CLI. The local backend applies the same migration files as a deployment. Its data stays on your computer. Run each command from your app’s folder while the local backend is running. To start the local backend, see [Local development workflow](backend-local-workflow.md).

## Apply a migration
<a name="_apply_a_migration"></a>

To create a migration file, run the following command. Replace `create notes` with a description of your change.

```bash
aws amplifygen3 local migration create --description "create notes"
```

You should see a new file such as `migrations/20260923040000_create_notes.sql`. It holds only the comment `-- create notes`. Write your SQL in the file, then run the following command to apply every pending migration to the local database:

```bash
aws amplifygen3 local migration apply
```

 `aws amplifygen3 local run` also applies pending migrations when it starts, such as the files that you pull with Git.

**Note**  
An apply records every pending file as applied, even a file that holds only its comment. Write the SQL before you run `local migration apply` or `local run`. If you edit a file after it’s applied, the next apply fails with `checksum_mismatch`.

For a one-line change, pass the SQL and a name. The CLI writes the file in `migrations/` and then applies every pending file:

```bash
aws amplifygen3 local migration apply --sql "create index notes_owner_idx on public.notes (owner_id)" --migration-name "index notes by owner"
```

For more information about writing a table, its grants and its row-level security (RLS) policies in a migration, see [Tables and migrations](backend-data.md).

## Check which migrations are applied
<a name="_check_which_migrations_are_applied"></a>

Run the following command to list each migration as `applied`, with the time it was applied, or `pending`:

```bash
aws amplifygen3 local migration list
```

When a new file sorts before a file already applied to the local database, `local migration apply` fails and applies nothing. This check covers only the files applied to the local database. A file that sorts before one applied only in an environment passes locally, and the deployment fails with `out of order`. For more information about renaming the file, see [Tables and migrations](backend-data.md).

## Run SQL
<a name="_run_sql"></a>

Run the following commands to run a query, run a SQL file, open an interactive `psql` session or save a dump of your schemas:

```bash
aws amplifygen3 local db query "select id, owner_id from public.notes limit 5"
aws amplifygen3 local db query --file checks.sql
aws amplifygen3 local db connect
aws amplifygen3 local db dump --output-file backup.sql
```

On a terminal, `local db query` prints a table. In a script, or with `--output json`, it prints the rows as JSON. Each write commits at once. Put schema changes in a migration, so that a deployment applies them too. You don’t need `psql` or `pg_dump` on your computer. For more information about how the CLI runs them, see [Docker and Finch](backend-local-runtimes.md).

The `local db` commands sign in as `amplify_admin`, with the password in `.secrets/db_password`. `amplify_admin` owns your tables and bypasses RLS, so its queries return every row. To test your RLS policies, see [Test your policies locally](#test-your-policies-locally).

## Connect a database client
<a name="_connect_a_database_client"></a>

To connect a GUI client, such as TablePlus or DBeaver, or an ORM tool, run the following command. It prints the connection URL, with the user `amplify_admin`, the database `amplify` and the local password.

```bash
aws amplifygen3 local db token --output text --query data.uri
```

 `aws amplifygen3 local init` adds `.secrets/` to `.gitignore`. Keep it there, so that the password stays out of your commits.

## Seed the local database
<a name="_seed_the_local_database"></a>

To run the statements in `seed.sql` against the local database, run the following command. Add `--file` to run another file.

```bash
aws amplifygen3 local backend seed
aws amplifygen3 local backend seed --file seeds/demo.sql
```

When a seed row belongs to a user, create the user first. The following command creates the user and reads the password from standard input:

```bash
printf %s "$DEMO_PASSWORD" | aws amplifygen3 local backend user create --email mary_major@example.com
```

For more information about writing a seed file that you can run more than once, and seeding rows for a user, see [Seed your database](backend-seed.md).

## Reset the local database
<a name="_reset_the_local_database"></a>

To delete your local data and users and apply every migration again, run the following command. Add `--seed` to run `seed.sql` after the migrations.

```bash
aws amplifygen3 local reset --seed
```

**Warning**  
 `local reset` drops every object in the `public` schema, such as tables, views, functions and types, and deletes the local users and their sessions. It also clears the migration record and the emails in Mailpit. Deployed environments keep their data.

On a terminal, the command asks you to confirm. In a script, it describes the reset, deletes nothing and exits with status 2. To reset from a script, add `--yes`.

The reset deletes the users that your seed rows select by email, so `--seed` skips those rows. To get them back, reset, create the user again, and then seed:

```bash
aws amplifygen3 local reset --yes
printf %s "$DEMO_PASSWORD" | aws amplifygen3 local backend user create --email mary_major@example.com
aws amplifygen3 local backend seed
```

## Generate TypeScript types
<a name="_generate_typescript_types"></a>

To generate types from the tables in the local database, run the following command. Replace `src/db.types.ts` with the file that your app imports its types from.

```bash
aws amplifygen3 local gen-types --output-file src/db.types.ts
```

The command replaces the file. Run it again after each migration. For more information about using the types with the client, see [Generate TypeScript types](backend-types.md).

## Test your policies locally
<a name="test-your-policies-locally"></a>

 `amplify_admin` can’t switch to the `anon` or `authenticated` role, so SQL from the `local db` commands doesn’t test your RLS policies. Test each policy through the data API as the owner, as a second user and signed out. Follow the steps in [Secure your data](backend-rls.md), with these local values:
+  **Backend URL** – The `local.endpoint` value in `amplify.local.json`. You can also print it with `aws amplifygen3 local status --output text --query data.endpoint`.
+  **Publishable key** – `hpk_v1.local`.
+  **Test users** – Create each test user with `aws amplifygen3 local backend user create`. When you’re done, delete each test user with `aws amplifygen3 local backend user delete --email <email> --yes`.

## Lint and inspect the database
<a name="_lint_and_inspect_the_database"></a>

Run the following command to report RLS errors in the `public` schema, such as a table that `anon` or `authenticated` can query while RLS is off (`rls_disabled_in_public`):

```bash
aws amplifygen3 local db lint --level error
```

To check a function in another schema, such as a security definer function in `private`, name both schemas. The command reports a `function_search_path_mutable` warning for each function that has no `set search_path`.

```bash
aws amplifygen3 local db lint --schema public,private
```

In a CI job, add `--fail-on error` to exit with status 1 on any error. After `local run` or `local migration apply` applies migrations, it prints the RLS findings as warnings. For more information about each finding, see [Secure your data](backend-rls.md).

To read table, index and lock statistics, run `aws amplifygen3 local db inspect <name>`, such as `local db inspect table-stats`. To save every statistic as a CSV file, run `aws amplifygen3 local db report --output-dir db-report`.

## Read the database log
<a name="_read_the_database_log"></a>

To read the PostgreSQL log of the local backend, such as the full error of a failed migration, run the following command:

```bash
aws amplifygen3 local logs --log-type database
```

The command prints JSON lines while the local backend is running.

## Differences from a deployed database
<a name="_differences_from_a_deployed_database"></a>

### Extensions
<a name="_extensions"></a>

To list the extensions that the local database has, run the following command:

```bash
aws amplifygen3 local db query "select name from pg_available_extensions order by name"
```

The local image doesn’t include `plv8`, `postgis` or `postgis_raster`, and it can lack others that a deployed backend allows. A migration that creates one of them stops `local run` with `migration_failed`, or `local migration apply` with `http_400`, and the message says `extension "<name>" is not available in the local backend`. Keep the statement in the migration, because a deployment applies it. The local backend can’t apply that file or any file after it, so do that schema work in `preview` or in an environment of your own. If a migration creates an extension that isn’t on the allowed list, the migration fails on the local backend with the same `permission denied to create extension` (`42501`) error as a deployment. For more information about the allowed list, see [Extensions](backend-extensions.md).

### Text sort order
<a name="_text_sort_order"></a>

The local database sorts text by code point (`C.UTF-8`), so `order by name` can return rows in a different order than a deployed database. To get the same order in both, name the collation, as in `order by name collate "C"`.

## Errors
<a name="_errors"></a>

The local database commands return the following errors:


| Error | Cause | Fix | 
| --- | --- | --- | 
|  `not_running`  | The local backend is stopped | Run `aws amplifygen3 local run`  | 
|  `migration_failed` from `local run`  | A migration failed. The message names the file and the PostgreSQL error. The local backend keeps running | Fix the file, then run `aws amplifygen3 local migration apply`  | 
|  `out_of_order` from `local migration apply`  | A new migration file sorts before a file already applied to the local database (HTTP 409). Nothing is applied | Rename the file so that it sorts after the applied files, then run `aws amplifygen3 local migration apply` again | 
|  `migration_conflict` from `local migration apply`  | The migration files conflict with the applied history in another way (HTTP 409). Nothing is applied | Reconcile the files with the applied history, then run `aws amplifygen3 local migration apply` again | 
|  `http_<code>` from `local migration apply`  | The SQL in the named file failed, such as `http_400`. Earlier files stay applied, and the failing file rolls back | Fix the file, and run `aws amplifygen3 local migration apply` again | 
|  `checksum_mismatch`  | You edited an applied migration | Undo the edit, and write the change in a new migration. To rebuild the local database from the edited files, run `aws amplifygen3 local reset`. The reset deletes your local data | 
|  `naming_error`  | A `.sql` file in `migrations/` isn’t named `<14-digit timestamp>_<name>.sql`  | Rename the file, or create it with `aws amplifygen3 local migration create`  | 
|  `seed_not_found`  |  `seed.sql` is missing | Create the file, or pass `--file`  | 
|  `interactive_only`  | You ran `local db connect` from a script | Run the SQL with `aws amplifygen3 local db query`  | 
|  `image_too_old`  | The local image is older than the CLI | Run `aws amplifygen3 local run --rebuild`  | 
| Exit status 2, `confirmation_required`  | You ran `local reset` from a script without `--yes`  | Confirm the reset, and run it again with `--yes`  | 
| Exit status 252, `argument --output: Found invalid choice`  | You passed `--out` to `local gen-types` or `local db dump`  | Use `--output-file`  | 