

# Extensions

An extension adds types, functions and operators to PostgreSQL, such as the `vector` type from pgvector. You turn on an extension in your Amplify backend database with a migration, and a deployment applies the migration to the environment that you deploy to.

## Turn on an extension
<a name="backend-extensions-turn-on"></a>

Add a `create extension` statement to a migration. Use the same form for every extension:

```sql
-- The quotes keep a name such as "uuid-ossp" valid.
-- "cascade" first installs the extensions that this one needs, such as cube
-- for earthdistance, and puts the languages pltcl and plv8 in
-- pg_catalog, where they must go.
create extension if not exists "vector" with schema extensions cascade;
```

Commit the migration, and run the following command to deploy it to `preview`:

```bash
aws amplifygen3 deploy --env preview
```

To turn on the extension in another environment, such as `production`, deploy the migration to that environment.

For more information about creating and deploying migrations, see [Tables and migrations](backend-data.md).

## Use an extension
<a name="backend-extensions-use"></a>

You use an extension’s types and functions in SQL without a schema name, because the `extensions` schema is on the search path of every database login. The following migration adds a `vector` column to a table:

```sql
create table public.documents (
  id        bigint generated always as identity primary key,
  body      text not null,
  embedding vector(3)
);
```

The data API serves only the `public` schema. To call an extension’s function from your app, wrap it in a function in `public`, and call that function. The following migration wraps `similarity` from `pg_trgm`:

```sql
create function public.search_projects(term text) returns setof public.projects
  language sql stable
  as $$ select * from public.projects order by similarity(name, term) desc limit 10 $$;

grant execute on function public.search_projects(text) to authenticated;
```

For more information about calling a function from your app, see [Call database functions](backend-data-api-rpc.md).

## Turn off an extension
<a name="backend-extensions-turn-off"></a>

To turn off an extension, write a migration that drops the objects that use it, and then drops the extension:

```sql
alter table public.documents drop column embedding;
drop extension if exists "vector";
```

## Allowed extensions
<a name="allowed-extensions"></a>

You can turn on the following 28 extensions in a deployed backend.


| Use | Extensions | Note | 
| --- | --- | --- | 
| Vector search |  `vector` (pgvector) |  | 
| Geospatial data |  `postgis`, `postgis_raster`, `earthdistance`  |  `postgis_raster` needs `postgis`, and `earthdistance` needs `cube`. `cascade` installs them. | 
| Text search and matching |  `pg_trgm`, `fuzzystrmatch`, `unaccent`, `dict_int`, `citext`  |  | 
| Data types |  `cube`, `hstore`, `ltree`, `isn`, `seg`, `intarray`, `hll`, `lo`  |  | 
| Indexes and query plans |  `btree_gin`, `btree_gist`, `hypopg`  |  | 
| IDs and cryptography |  `uuid-ossp`, `pgcrypto`  |  | 
| Procedural languages |  `pltcl`, `plv8`  |  `cascade` installs them in `pg_catalog`. | 
| Crosstab queries |  `tablefunc`  |  | 
| Change notifications |  `tcn`  |  | 
| Table sampling |  `tsm_system_rows`, `tsm_system_time`  |  | 

 `pg_available_extensions` also lists extensions that aren’t in this table, such as `pg_cron`, `dblink`, `postgres_fdw` and `pg_stat_statements`. A migration that creates one of them fails the deployment with `permission denied to create extension` (`42501`).

For the version of each extension, see [Extensions supported for Aurora PostgreSQL 17](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraPostgreSQLReleaseNotes/AuroraPostgreSQL.Extensions.html#AuroraPostgreSQL.Extensions.17).

PostgreSQL includes `gen_random_uuid()`, full-text search with `to_tsvector` and `to_tsquery`, and GIN indexes on `jsonb` columns, with no extension to turn on.

## Extensions on the local backend
<a name="backend-extensions-local"></a>

For the allowed extensions that the local backend includes, and how to work with the others, see [Local database](backend-local-database.md).

## Extension errors
<a name="backend-extensions-errors"></a>


| Error | Cause | Fix | 
| --- | --- | --- | 
|  `permission denied to create extension "<name>"` (`42501`), with a hint that names `rds.allowed_delegated_extensions`  | The extension isn’t in [Allowed extensions](#allowed-extensions). The deployment fails | Use an allowed extension, or remove the statement | 
|  `required extension "<name>" is not installed` (`42704`), with the hint `Use CREATE EXTENSION …​ CASCADE`  |  `create extension` without `cascade` for `earthdistance`, `postgis_raster`, or `plv8`  | Add `cascade` to the statement | 
|  `extension "<name>" must be installed in schema "pg_catalog"` (`0A000`) |  `create extension …​ with schema extensions` without `cascade` for `pltcl`, whose control file pins `pg_catalog`  | Add `cascade` to the statement | 
|  `permission denied to drop extension "<name>" with cascade` (`42501`) |  `drop extension …​ cascade`  | Drop the objects that use the extension, then drop the extension without `cascade`  | 

For other migration errors, see [Tables and migrations](backend-data.md).