---
title: "Export and import"
url: "https://docs.yugabyte.com/stable/manage/backup-restore/export-import-data-ysql/"
---

# Export and import

Export and import for YSQL

- [YSQL](/stable/manage/backup-restore/export-import-data-ysql)
- [YCQL](/stable/manage/backup-restore/export-import-data-ycql)

YugabyteDB includes `ysql_dump` and `ysql_dumpall` utilities that allow you to export data into a SQL script. These utilities derive from PostgreSQL `pg_dump` and `pg_dumpall`.

In general, it is recommended to use other means for backups, such as [distributed backup and recovery](/stable/manage/backup-restore/snapshot-ysql/ "distributed backup and recovery"). However, you can use `ysql_dump` and `ysql_dumpall` if you intend to restore the data on a database other than YugabyteDB, or if having data in SQL format is a requirement for other reasons, such as regulations.

Both utilities are thread-safe and always produce a consistent version of the database even if run concurrently with other applications that read and update the data.

For more details and a list of all available options, see the following:

- [ysql\_dump reference](/stable/admin/ysql-dump/ "ysql_dump reference")
- [ysql\_dumpall reference](/stable/admin/ysql-dumpall/ "ysql_dumpall reference")

## Export a single database

To export a single database with all its tables, indexes, and other local artifacts, use the `ysql_dump` utility by executing the following command:

```sh
./postgres/bin/ysql_dump -d <db-name> > <file>
```

- *db-name* is the name of the database to be exported.
- *file* is the path to the resulting SQL script file.

For example, to export the `mydb` database into a file called `mydb-dump.sql` located in the `backup` folder, the command would be as follows:

```sh
./postgres/bin/ysql_dump -d mydb > backup/mydb-dump.sql
```

## Export a single table

To export a single database table, use the `ysql_dump` utility by executing the following command:

```sh
./postgres/bin/ysql_dump -t <table-name> <db-name> -f <file>
```

- *table-name* is the name of the table to be exported.
- *db-name* is the name of the database to be exported.
- *file* is the path to the resulting SQL script file.

For example, to export the `mytable` table of the `mydb` database into a file called `mytable-mydb-dump.sql` located in the `backup` folder, the command would be as follows:

```sh
./postgres/bin/ysql_dump -t mytable mydb -f backup/mytable-mydb-dump.sql
```

## Export all databases

To export all databases, along with the global artifacts such as users, roles, permissions, and so on, use the `ysql_dumpall` utility by executing the following command:

```sh
./postgres/bin/ysql_dumpall > <file>
```

*file* is the path to the resulting SQL script file.

The following are two of the script's common command-line options:

- *--roles-only* exports just the roles.
- *--schema-only* exports all database objects without the data.

## Import

You can import schemas and objects into YugabyteDB from a SQL script. To create this script, follow instructions provided in [Export a single database](#export-a-single-database "Export a single database") or [Export all databases](#export-all-databases "Export all databases"). Alternatively, you can obtain the script from an external database that supports PostgreSQL syntax.

To import the script, use the ysqlsh command line tool, as follows:

```sh
./bin/ysqlsh -f <sql_script>
```

*sql\_script* is the path to the SQL script to be imported.

Dumps taken with ysql\_dump or ysql\_dumpall in v2025.2.7.0+ or v2026.1.2.0+ begin with the [`\restrict`](/stable/api/ysqlsh-meta-commands/#restrict-restrict "\restrict") meta-command. Replay them with ysqlsh from the same or a later version. See [ysql\_dump Notes](/stable/admin/ysql-dump/#notes "ysql_dump Notes").

You can also use the `\i` meta-command in the ysqlsh shell to import a SQL script, as follows:

```sql
yugabyte=# \i <sql_script>
```
