To move databases into or out of AlloyDB for PostgreSQL, you can migrate active
PostgreSQL instances with minimal downtime using Database Migration Service or
import and export CSV, DMP, and SQL files stored in Cloud Storage.

## Importing data

You can migrate data into AlloyDB through two methods:

- Manually importing files containing your data.

- Using Database Migration Service to migrate the contents of a whole,
  active PostgreSQL instance into a new AlloyDB cluster. For more
  information, see [Migrate to AlloyDB for PostgreSQL using Database Migration Service](https://docs.cloud.google.com/alloydb/docs/migrate-with-dms.md.txt).

> [!CAUTION]
> **Caution:** If your source database uses a default collation based on a locale provided by `libc`, you might need to add ICU-based collations to your tables after importing their data into AlloyDB. For more information, see [Supported locales for imported data](https://docs.cloud.google.com/alloydb/docs/migration-overview.md.txt#locales).

### Import archived data from files

AlloyDB lets you import
data from files located in a Cloud Storage bucket.
AlloyDB supports a variety of file formats for data
import, including the following:

- [CSV](https://docs.cloud.google.com/alloydb/docs/import-csv-file.md.txt), with the contents of one table
  per file, loaded using `psql`.

- [DMP](https://docs.cloud.google.com/alloydb/docs/import-dmp-file.md.txt), a binary archive of a whole
  PostgreSQL database, imported using `pg_restore`.

- [SQL](https://docs.cloud.google.com/alloydb/docs/import-sql-file.md.txt), a plain-text reconstruction of a
  PostgreSQL database, processed with `psql`.

### Migrate an active instance with Database Migration Service

As an alternative to manually importing data-dump files, you can use
[Database Migration Service for AlloyDB](https://docs.cloud.google.com/database-migration/docs/postgresql-to-alloydb/migration-src-and-dest.md.txt).
This service lets you migrate the entire contents of an
active PostgreSQL instance---including all of its databases and
metadata---into a new AlloyDB cluster. Database Migration Service can
help you transition your existing applications to using
AlloyDB as their new datastore with minimal downtime.

### Supported locales for imported data

AlloyDB supports the following
[locales](https://www.postgresql.org/docs/18//locale.html):

- The full set of ICU-provided locales.
- A limited set of locales provided by `libc`:
  - `C.UTF-8`
  - `en_US.utf8`

If the database you want to import into AlloyDB defaults
to a `libc` locale other than the two listed here, you can still import
your data, but that default won't carry over. To ensure that SQL queries
using `ORDER BY` sort their results properly, you might need to take
additional steps after importing your data.

Google recommends allowing your new database to use the default locale for
AlloyDB: `C.UTF-8`, provided by `libc`. Then, associate
[collations](https://www.postgresql.org/docs/18//collation.html) with any
columns that your application's queries might involve in `ORDER BY`
clauses, naming an appropriate ICU-based collation for each one. You can
do this through [`ALTER TABLE` DDL
queries](https://www.postgresql.org/docs/18//sql-altertable.html), such as the following:

```sql
ALTER TABLE TABLE_NAME
    ALTER COLUMN COLUMN_NAME
    SET DATA TYPE COLUMN_DATA_TYPE
    COLLATE "COLLATION_NAME";
```

Replace the following:

- `TABLE_NAME`: the name of the table containing the column.
- `COLUMN_NAME`: the name of the column to update.
- `COLUMN_DATA_TYPE`: the data type of the column.
- `COLLATION_NAME`: the name of the ICU-based collation to apply.

A new AlloyDB cluster defines hundreds of collations
based on ICU-provided locales, and you can add more using [the PostgreSQL
`CREATE COLLATION` facility](https://www.postgresql.org/docs/18//sql-createcollation.html). To
see the full list of ICU-based collation names defined on an
AlloyDB cluster,
run the following query on any of its instances:

```sql
SELECT collname FROM pg_collation WHERE collprovider = 'i';
```

In addition to ICU-based collations, AlloyDB supports the
PostgreSQL built-in collation named `ucs_basic`. This collation uses
the standard order of Unicode code points to enable especially efficient
sorting. Google recommends its use with columns whose appropriate sort order
matches that of the Unicode code-point list.

## Exporting data

You can use command-line utilities to export your AlloyDB
data into files stored on a Cloud Storage bucket, in a variety of formats:

- [CSV](https://docs.cloud.google.com/alloydb/docs/export-csv-file.md.txt), exporting one table per file,
  using `psql`.

- [DMP](https://docs.cloud.google.com/alloydb/docs/export-dmp-file.md.txt), using `pg_dump` to create a
  portable, binary archive of your whole database.

- [SQL](https://docs.cloud.google.com/alloydb/docs/export-sql-file.md.txt), using `pg_dump` to create a list
  of DDL and SQL statements to reconstruct your database.

## What's next

- [Migrate data from a vector database to AlloyDB using LangChain](https://docs.cloud.google.com/alloydb/docs/ai/migrate-data-from-langchain-vector-stores-to-alloydb.md.txt).