Project: YugabyteDB (App Definition and Development / Database)
File: restore-ysql-single-table.md

title: Restore a single YSQL table headerTitle: Restore a single YSQL table linkTitle: Restore a single YSQL table description: Use YugabyteDB Anywhere to restore data. headContent: Restore from full or incremental backups menu: v2.18_yugabyte-platform: parent: restore-universe-data identifier: restore-ysql-single-table weight: 30 type: docs Currently, YugabyteDB Anywhere supports backing up and restoring entire YSQL databases only. However, by performing some manual steps, you can restore single tables to a YSQL database using the following tools: - exports data from a YSQL database into a SQL script file. - YugabyteDB SQL shell for interacting with YugabyteDB using YSQL. Suppose you have a problem with a single table in a database called source_db, and you want to restore the table from a backup taken previously. You would perform the following steps: Using a backup of sourcedb, restore the backup to a new database (call it restoreddb). This can be done on the same or a different universe. In restoreddb, use ysqldump to export a SQL script file of the table to be restored. In source_db, use ysqlsh to drop the table with the same name (that is, the table you want restored from the backup). (You only need to do this if the table with the same name exists on the database.) Use ysqlsh to import the SQL script file to the source_db database. The scope of the following procedure is limited to restoring single tables that don't have foreign key relations with other tables. In general, restoring single tables with relations with other tables is difficult, owing to the fact that you may encounter referential integrity issues. The following example assumes the following basic setup: a YSQL database named source_db. sourcedb has 3 tables (table1, table2, and table3) and the tables do not have any foreign key references. source_db has been backed up using YugabyteDB Anywhere. You can restore the backup to the same or to a different universe. This example assumes that you restored to the same universe. During the restore, rename the database. For example, you would rename sourcedb to

Because YugabyteDB Anywhere backs up and restores only full databases in YSQL, restoreddb has the same tables and data as sourcedb at the time of backup. Use ysqldump to create the script file for the table you want to restore. ysqldump is located in the `postgres/bin` directory of the YugabyteDB home directory on any of the nodes of your universe. Do the following: Connect to one of the nodes of the universe where you restored the backup (that is, the universe with restoreddb) and change to the directory where ysqldump is located. For example: ```sh cd /home/yugabyte/tserver/postgres/bin ``` Use ysql_dump to export the table to a SQL script file. The syntax for dumping a single table is as follows: ```sh ./ysql_dump -h <host> -t <table-name> <database-name> -f <file-path> ``` Replace values as follows: host is the host IP of the universe node. table-name is the name of the table (table_1) to export. database-name is the name of the database (restored_db). file-path is the path to the resulting SQL script file. For example: ```sh ./ysqldump -h 172.16.0.0 -t table1 restoreddb -f /home/yugabyte/restoreddbtable1.sql ``` Before you can restore the table, you need to drop table1 from the sourcedb database. To do this, you can use ysqlsh, located in the `/bin` directory of the YugabyteDB home directory. Do the following to drop a table from the source database source_db: Connect to one of the nodes of the universe with the source database (that is, the universe with source_db) and start ysqlsh. For example: ```sh ./tserver/bin/ysqlsh -h 172.16.0.0 ``` Connect to the source_db database: ```sql yugabyte=# \c source_db ``` Drop table table_1: ```sql sourcedb=# DROP TABLE table1; ``` ```output DROP TABLE ``` ```sql source_db=# \d ``` ```output List of relations Schema | Name | Type | Owner --++-+- public | table_1 | table | yugabyte public | table_2 | table | yugabyte (2 rows) ``` You import the SQL script file to the sourcedb database using the ysqlsh `\i` meta-command. For example, assuming restoreddb was restored to the universe with source_db, do the following: ```sql sourcedb=# \i /home/yugabyte/restoredbtable1.sql source_db=# \d ``` ```output List of relations Schema | Name | Type | Owner --++-+- public | table_1 | table | yugabyte public | table_2 | table | yugabyte public | table_3 | table | yugabyte (3 rows) ```