Wenn Sie Ihr Schema, Ihre Daten und Ihre Metadaten aus einer Quelldatenbank in eine Zieldatenbank migrieren, sollten Sie darauf achten, dass alle diese Informationen korrekt migriert werden. Der Database Migration Service bietet eine zuverlässige Möglichkeit, Datenbankobjekte (einschließlich Schema, Daten und Metadaten) von einer Datenbank in eine andere zu migrieren.
Während des Migrationsprozesses werden Daten und Einschränkungen separat migriert.
Zuerst werden die Daten migriert. Einschränkungen wie Primärschlüssel, Fremdschlüssel und Indexe werden nach dem anfänglichen vollständigen Dump und Laden in der Instanz neu erstellt.
Im Rahmen der Datenbankmigration werden alle folgenden Daten-, Schema- und Metadatenkomponenten migriert:
Daten
Alle Tabellen aus allen Datenbanken und Schemas, mit Ausnahme der folgenden Schemas:
Das Informationsschema information_schema
Alle Schemas, die mit pg beginnen (z. B. pg_catalog)
Bei kontinuierlichen Migrationen werden nur DML-Änderungen (Data Manipulation Language) automatisch aktualisiert. Die Verwaltung von Änderungen an der Datendefinitionssprache (DDL), damit die Quell- und Zieldatenbanken kompatibel bleiben, liegt in der Verantwortung des Nutzers. Dies kann auf zwei Arten erfolgen:
Beenden Sie Schreibvorgänge in die Quelle und führen Sie die DDL-Befehle in der Quelle und dem Ziel aus. Gewähren Sie dem Cloud SQL-Nutzer, der die DDL-Änderungen anwendet, die Rolle cloudsqlexternalsync, bevor die DDL-Befehle für das Ziel ausgeführt werden. Gewähren Sie den entsprechenden Cloud SQL-Nutzern die Rolle cloudsqlexternalsync, um Abfragen oder Änderungen der Daten zu ermöglichen.
Verwenden Sie die pglogical.replicate_ddl_command, damit DDL an der Quelle und am Ziel an einem konsistenten Punkt ausgeführt werden kann. Der Nutzer, der diesen Befehl ausführt, muss sowohl an der Quelle als auch am Ziel denselben Nutzernamen haben und Superuser oder Inhaber des zu migrierenden Artefakts sein (z. B. der Tabelle, Sequenz, Ansicht oder Datenbank).
Hier einige Beispiele für die Verwendung von pglogical.replicate_ddl_command.
Ersetzen Sie:
[SCHEMA] durch den Namen des zu verwendenden Tabellenschemas
[TABLE_NAME] durch den Tabellennamen
[NEW_NAME_FOR_TABLE] durch den neuen Namen der Tabelle beim Umbenennen
Datenbanktabelle mit einem Primärschlüssel eine Spalte hinzufügen
Namen einer Datenbanktabelle mit einem Primärschlüssel ändern
selectpglogical.replicate_ddl_command('ALTER TABLE [SCHEMA].[TABLE_NAME] RENAME TO [NEW_NAME_FOR_TABLE]','{default}');
Namen einer Datenbanktabelle ohne Primärschlüssel ändern
selectpglogical.replicate_ddl_command('ALTER TABLE [SCHEMA].[TABLE_NAME] RENAME TO [NEW_NAME_FOR_TABLE]','{default_insert_only}');
Datenbanktabelle mit einem Primärschlüssel erstellen
Führen Sie folgende Befehle aus:
selectpglogical.replicate_ddl_command(command:='CREATE TABLE [SCHEMA].[TABLE_NAME] (id INTEGER PRIMARY KEY, name VARCHAR);',replication_sets:=ARRAY['default']);
selectpglogical.replicate_ddl_command(command:='CREATE TABLE [SCHEMA].[TABLE_NAME] (id INTEGER PRIMARY KEY, name VARCHAR);',replication_sets:=ARRAY['default_insert_only']);
Der Database Migration Service migriert keine Erweiterungen, die von Cloud SQL nicht unterstützt werden. Die Migration wird durch diese Erweiterungen nicht blockiert. Für einen reibungslosen Migrationsprozess sollten Sie jedoch prüfen, ob Ihre Objekte oder Anwendungen keine nicht unterstützten Erweiterungen enthalten. Wir empfehlen, diese Erweiterungen und Verweise aus Ihrer Quelldatenbank zu entfernen, bevor Sie fortfahren.
Große Objekte können nicht repliziert werden, da die logische Decodierungsfunktion von PostgreSQL keine Decodierung von Änderungen an großen Objekten unterstützt. Bei Tabellen mit dem Spaltentyp oid, der auf große Objekte verweist, werden die Zeilen synchronisiert und neue Zeilen repliziert.
Der Zugriff auf das Large Object in der Zieldatenbank (Lesen mit lo_get, Exportieren mit lo_export oder Prüfen des Katalogs pg_largeobject auf die angegebene oid) schlägt jedoch fehl und es wird die Meldung angezeigt, dass das Large Object nicht vorhanden ist.
Bei Tabellen ohne Primärschlüssel unterstützt Database Migration Service die Migration des ersten Snapshots und der INSERT-Anweisungen während der CDC-Phase (Change Data Capture). Sie sollten UPDATE- und DELETE-Anweisungen manuell migrieren.
Der Database Migration Service migriert keine Daten aus materialisierten Ansichten, sondern nur das Ansichtsschema. Führen Sie den folgenden Befehl aus, um die Daten in die Ansichten einzufügen: REFRESH MATERIALIZED VIEW view_name.
Die SEQUENCE-Status (z. B. last_value) des neuen Ziels können von den SEQUENCE-Status der Quelle abweichen.
Benutzerdefinierte Tablespaces werden in der Ziel-Cloud SQL-Instanz nicht unterstützt. Alle Daten in benutzerdefinierten Tablespaces werden in Cloud SQL in den Standard-Tablespace pg_default migriert.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-05 (UTC)."],[[["\u003cp\u003eDatabase Migration Service ensures high-fidelity migration of database objects, including schema, data, and metadata, from source to destination databases.\u003c/p\u003e\n"],["\u003cp\u003eDuring migration, data is migrated first, followed by the recreation of constraints such as primary keys, foreign keys, and indexes on the destination instance.\u003c/p\u003e\n"],["\u003cp\u003eContinuous migration automatically updates data manipulation language (DML) changes, while data definition language (DDL) changes require manual management or the use of \u003ccode\u003epglogical.replicate_ddl_command\u003c/code\u003e to maintain compatibility.\u003c/p\u003e\n"],["\u003cp\u003eCertain components are not migrated, such as unsupported Cloud SQL extensions, large objects, \u003ccode\u003eUPDATE\u003c/code\u003e and \u003ccode\u003eDELETE\u003c/code\u003e statements for tables without primary keys, and data from materialized views, where users will have to refresh it on the destination end.\u003c/p\u003e\n"],["\u003cp\u003eAll data in customized tablespaces will migrate to the default \u003ccode\u003epg_default\u003c/code\u003e tablespace in Cloud SQL, and \u003ccode\u003eSEQUENCE\u003c/code\u003e states may differ from the source to the destination.\u003c/p\u003e\n"]]],[],null,["# Migration fidelity\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n[MySQL](/database-migration/docs/mysql/migration-fidelity \"View this page for the MySQL version of Database Migration Service.\") \\| PostgreSQL \\| [PostgreSQL to AlloyDB](/database-migration/docs/postgresql-to-alloydb/migration-fidelity \"View this page for the PostgreSQL to AlloyDB version of Database Migration Service.\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nOverview\n--------\n\nWhen you're migrating your schema, data, and metadata from a source database to a\ndestination database, you want to ensure that all of this information is\nmigrated accurately. Database Migration Service provides a high-fidelity way to\nmigrate database objects (including the schema, data, and metadata) from one\ndatabase to another.\nDuring the migration process, data and constraints are migrated separately. Data is migrated first, and constraints such as primary keys, foreign keys, and indexes are recreated on the instance after the initial full dump and load.\n\nAll of the following data, schema, and metadata components are migrated as part\nof the database migration:\n\n### Data\n\n- All tables from all databases and schemas, excluding the following schemas:\n\n - The information schema `information_schema`\n - Any schemas beginning with `pg` (for example, `pg_catalog`)\n\n For more information about these schemas, see [Known limitations](/database-migration/docs/postgres/known-limitations).\n\n### Schema\n\n- Naming\n\n- Primary key\n\n- Data type\n\n- Ordinal position\n\n- Default value\n\n- Nullability\n\n- Auto-increment attributes\n\n- Secondary indexes\n\n### Metadata\n\n- Stored procedures\n\n- Functions\n\n- Triggers\n\n- Views\n\n- Foreign key constraints\n\n### Continuous migration\n\nOnly data manipulation language (DML) changes are updated automatically during\ncontinuous migrations. Managing data definition language (DDL) changes so that the source and destination databases remain compatible is the\nresponsibility of the user, and can be achieved in two ways:\n\n1. Stopping writes to the source and running the DDL commands in both source and destination. Before running DDL commands on the destination, grant `cloudsqlexternalsync` to the Cloud SQL user applying the DDL changes. To enable querying or changing the data, grant the `cloudsqlexternalsync` role to the relevant Cloud SQL users.\n2. Using the `pglogical.replicate_ddl_command` to allow DDL to be run on the source and destination at a consistent point. The user running this command must have the same username on both the source and the destination, and should be the superuser or the owner of the artifact being migrated (for example, the table, sequence, view, or database).\n\n Here are a few examples of using the `pglogical.replicate_ddl_command`.\n\n Replace:\n - \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e with the name of the table schema you want to use\n - \u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e with the table name\n - \u003cvar label=\"new_table_name\" translate=\"no\"\u003e[NEW_NAME_FOR_TABLE]\u003c/var\u003e with the new name for the table when when performing the rename operation\n\n #### Add a column to a database table with a primary key\n\n \u003cbr /\u003e\n\n ```sql\n select pglogical.replicate_ddl_command(\n 'ALTER TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e add column surname varchar(20)',\n '{default}'\n );\n ```\n\n \u003cbr /\u003e\n\n #### Add a column to a database table without a primary key\n\n \u003cbr /\u003e\n\n ```sql\n select pglogical.replicate_ddl_command(\n 'ALTER TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e add column surname varchar(20)',\n '{default_insert_only}'\n );\n ```\n\n \u003cbr /\u003e\n\n #### Change the name of a database table with a primary key\n\n \u003cbr /\u003e\n\n ```sql\n select pglogical.replicate_ddl_command(\n 'ALTER TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e RENAME TO \u003cvar label=\"new_table_name\" translate=\"no\"\u003e[NEW_NAME_FOR_TABLE]\u003c/var\u003e',\n '{default}'\n );\n ```\n\n \u003cbr /\u003e\n\n #### Change the name of a database table without a primary key\n\n \u003cbr /\u003e\n\n ```sql\n select pglogical.replicate_ddl_command(\n 'ALTER TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e RENAME TO \u003cvar label=\"new_table_name\" translate=\"no\"\u003e[NEW_NAME_FOR_TABLE]\u003c/var\u003e',\n '{default_insert_only}'\n );\n ```\n\n \u003cbr /\u003e\n\n #### Create a database table with a primary key\n\n Run the following commands:\n 1.\n\n ```sql\n select pglogical.replicate_ddl_command(\n command := 'CREATE TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e\n (id INTEGER PRIMARY KEY, name VARCHAR);',\n replication_sets := ARRAY['default']\n );\n ```\n 2.\n\n ```sql\n select pglogical.replication_set_add_table('default', '\u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e');\n ```\n\n \u003cbr /\u003e\n\n #### Create a database table without a primary key\n\n Run the following commands:\n 1.\n\n ```sql\n select pglogical.replicate_ddl_command(\n command := 'CREATE TABLE \u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e\n (id INTEGER PRIMARY KEY, name VARCHAR);',\n replication_sets := ARRAY['default_insert_only']\n );\n ```\n 2.\n\n ```sql\n select pglogical.replication_set_add_table(\n 'default_insert_only', '\u003cvar label=\"schema\" translate=\"no\"\u003e[SCHEMA]\u003c/var\u003e.\u003cvar label=\"table\" translate=\"no\"\u003e[TABLE_NAME]\u003c/var\u003e'\n );\n ```\n\n \u003cbr /\u003e\n\n### What isn't migrated\n\n- To add users to a Cloud SQL destination instance, navigate to the instance and add users\n from the **Users** tab, or add them from a PostgreSQL client. Learn more about [creating\n and managing PostgreSQL users](https://cloud.google.com/sql/docs/postgres/create-manage-users).\n\n- Database Migration Service doesn't migrate [extensions that are unsupported by\n Cloud SQL](/sql/docs/postgres/extensions). The presence of these extensions\n doesn't block the migration, but to ensure a smooth migration process\n verify that your objects or applications don't reference any unsupported\n extensions. We recommend removing these extensions and references from your\n source database before you proceed.\n\n\u003c!-- --\u003e\n\n- [Large objects](https://www.postgresql.org/docs/current/largeobjects.html)\n can't be replicated, as PostgreSQL's logical decoding facility does not support\n decoding changes to large objects. For tables that have column type\n [`oid`](https://www.postgresql.org/docs/current/datatype-oid.html)\n referencing large objects, the rows are synced, and new rows are replicated.\n However, trying to access the large object on the destination database\n (read using [`lo_get`](https://www.postgresql.org/docs/current/lo-funcs.html),\n export using [`lo_export`](https://www.postgresql.org/docs/current/lo-funcs.html),\n or check the catalog `pg_largeobject` for the given\n `oid`), fails with a message saying that the large object does\n not exist.\n\n- For tables that don't have primary keys, Database Migration Service supports migration of the **initial snapshot and `INSERT` statements during the change data capture (CDC) phase** . You should migrate `UPDATE` and `DELETE` statements manually.\n\n- Database Migration Service doesn't migrate data from materialized views, just the view schema. To populate the views, run the following command: `REFRESH MATERIALIZED VIEW `\u003cvar translate=\"no\"\u003eview_name\u003c/var\u003e.\n\n- The `SEQUENCE` states (for example, `last_value`) on the new destination might vary from the source `SEQUENCE` states.\n\n- Customized tablespaces aren't supported in the destination Cloud SQL instance. All the data inside customized tablespaces is migrated to the default `pg_default` tablespace in Cloud SQL."]]