Project: YDB (App Definition and Development / Database)
File: pg-dump.md

Data from PostgreSQL can be migrated to YDB using utilities such as , , and . The and utilities are installed with PostgreSQL. is YDB's command-line client, which is . To do this, you need to: Create a data dump using with the following parameters: `--inserts` to add data using the command, instead of the protocol. `--column-inserts` to add data using the command with column names. `--rows-per-insert=1000` to insert data in batches to speed up the process. `--encoding=utf_8` YDB only supports string data in . Convert the dump to a format supported by YDB using the `ydb tools pg-convert` command from . Load the result into YDB in PostgreSQL compatibility mode. The `ydb tools pg-convert` command reads a dump file or standard input created by the utility, performs transformations, and outputs to standard output a dump that can be sent to YDB's PostgreSQL-compatible middleware. `ydb tools pg-convert` performs the following transformations: Moving the creation of the primary key into the body of the command. Removing the `public` schema from table names. Deleting the `WITH (...)` section in `CREATE TABLE`. Commenting out unsupported constructs (optionally): `SELECT pg_catalog.set_config.` `ALTER TABLE` If the CLI cannot find a table's primary key, it will automatically create a column named `ydbstubid` as the primary key. The general form of the command: ```bash {{ ydb-cli }} [global options...] tools pg-convert [options...] ``` `global options` . `options` . | Name | Description | |--|-| | `-i` | The name of the file containing the original dump. If the option is not specified, the dump is read from standard input. | | `--ignore-unsupported`| When this option is specified, unsupported constructs will be commented out in the resulting dump and duplicated in standard error. By default, if unsupported constructs are detected, the command returns an error. This does not apply to `ALTER TABLE` expressions that define a table's primary key, as they are commented out in any case. | {% note warning %} When loading large dumps, reading from standard input is not recommended because the entire dump will be stored in RAM. It is advised to use the file option, in which case the CLI will only keep a small portion of the dump in memory. {% endnote %} As an example, data generated by will be loaded. Start Docker containers with PostgreSQL and YDB: ```bash docker run -d --rm -e POSTGRESUSER=root -e POSTGRESPASSWORD=1234 \ -e POSTGRES_DB=local --name postgres postgres:14 docker run --name ydb-postgres -d --pull always -p 5432:5432 -p 8765:8765 \ -e POSTGRESUSER=root -e POSTGRESPASSWORD=1234 \ -e YDBFEATUREFLAGS=enabletemptables \ -e YDBTABLEENABLEPREPAREDDDL=true \ -e YDBUSEINMEMORYPDISKS=true \ ghcr.io/ydb-platform/ydb-local:latest ``` Generate data through : ```bash docker exec postgres pgbench -i postgres://root:1234@localhost/local ``` Create a dump of the database using and load it into YDB: ```bash docker exec postgres pg_dump -U root --format=c --file=/var/lib/postgresql/data/dump.sql local docker cp postgres:/var/lib/postgresql/data/dump.sql . cat dump.sql | docker exec -i ydb-postgres psql -U root -d local ```