Configurer une base de données Cloud SQL pour PostgreSQL
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Les sections suivantes expliquent comment configurer une base de données Cloud SQL pour PostgreSQL.
Activer la réplication logique
Accédez à Cloud SQL dans la console Google Cloud .
Ouvrez l'instance Cloud SQL et cliquez sur MODIFIER.
Accédez à la section Options.
Cliquez sur AJOUTER UNE OPTION.
Dans le menu déroulant, sélectionnez l'option cloudsql.logical_decoding.
Définissez la valeur de l'option sur activé.
Cliquez sur ENREGISTRER pour enregistrer les modifications. Vous devrez redémarrer l'instance pour y appliquer les modifications.
Une fois votre instance redémarrée, vérifiez les modifications sous Options de base de données sur la page Présentation.
Créer une publication et un emplacement de réplication
Connectez-vous à la base de données en tant qu'utilisateur disposant des droits suffisants pour créer un emplacement de réplication. Sinon, exécutez la commande suivante :
ALTER USER USER_NAME WITH REPLICATION;
Remplacez les éléments suivants :
USER_NAME : nom de l'utilisateur auquel vous souhaitez accorder des droits de réplication.
Créez une publication. Nous vous recommandons de créer une publication uniquement pour les tables que vous souhaitez répliquer. Cela permet à Datastream de lire uniquement les données pertinentes, ce qui réduit la charge sur la base de données et Datastream :
CREATE PUBLICATION PUBLICATION_NAME
FOR TABLE SCHEMA1.TABLE1, SCHEMA2.TABLE2;
Remplacez les éléments suivants :
PUBLICATION_NAME : nom de votre publication. Vous devrez indiquer ce nom lorsque vous créerez un flux dans l'assistant de création de flux Datastream.
SCHEMA : nom du schéma contenant la table.
TABLE : nom de la table que vous souhaitez répliquer.
Vous pouvez également créer une publication pour toutes les tables de votre base de données. Notez que cette approche augmente la charge sur la base de données source et sur Datastream :
CREATE PUBLICATION PUBLICATION_NAME FOR ALL TABLES;
Créez un emplacement de réplication en saisissant la commande SQL suivante :
REPLICATION_SLOT_NAME : nom de votre emplacement de réplication.
Vous devrez indiquer ce nom lorsque vous créerez un flux dans l'assistant de création de flux Datastream.
Créer un utilisateur Datastream
Connectez-vous à la base de données à l'aide d'un client PostgreSQL.
Saisissez la commande PostgreSQL suivante :
CREATE USER USER_NAME WITH REPLICATION LOGIN PASSWORD 'USER_PASSWORD';
Remplacez les éléments suivants :
USER_NAME : nom de l'utilisateur Datastream que vous souhaitez créer.
USER_PASSWORD : mot de passe de connexion pour l'utilisateur Datastream que vous souhaitez créer.
Accordez les droits suivants à l'utilisateur que vous avez créé :
GRANT SELECT ON ALL TABLES IN SCHEMA SCHEMA_NAME TO USER_NAME;
GRANT USAGE ON SCHEMA SCHEMA_NAME TO USER_NAME;
ALTER DEFAULT PRIVILEGES IN SCHEMA SCHEMA_NAME
GRANT SELECT ON TABLES TO USER_NAME;
Remplacez les éléments suivants :
SCHEMA_NAME : nom du schéma auquel vous souhaitez accorder les privilèges.
USER_NAME : utilisateur auquel vous souhaitez accorder les droits d'accès.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eEnable logical replication in Cloud SQL by setting the \u003ccode\u003ecloudsql.logical_decoding\u003c/code\u003e flag to 'on' and restarting the instance to apply the changes.\u003c/p\u003e\n"],["\u003cp\u003eCreate a publication for specific tables or all tables, using the \u003ccode\u003eCREATE PUBLICATION\u003c/code\u003e command, to manage the data intended for replication.\u003c/p\u003e\n"],["\u003cp\u003eEstablish a replication slot with a unique name using the \u003ccode\u003ePG_CREATE_LOGICAL_REPLICATION_SLOT\u003c/code\u003e function for each stream replicating from the database.\u003c/p\u003e\n"],["\u003cp\u003eCreate a dedicated Datastream user with replication and login privileges, then grant it necessary \u003ccode\u003eSELECT\u003c/code\u003e and \u003ccode\u003eUSAGE\u003c/code\u003e privileges on the required schemas.\u003c/p\u003e\n"]]],[],null,["# Configure a Cloud SQL for PostgreSQL database\n\nThe following sections cover how to configure a Cloud SQL for PostgreSQL database.\n\n### Enable logical replication\n\n1. Navigate to Cloud SQL in the Google Cloud console.\n\n2. Open the Cloud SQL instance and click **EDIT**.\n\n3. Go to the **Flags** section.\n\n4. Click **ADD FLAG**.\n\n5. Choose the `cloudsql.logical_decoding` flag from the drop-down menu.\n\n6. Set the flag value to **on**.\n\n7. Click **SAVE** to save your changes. You'll need to restart your instance to\n update your instance with the changes.\n\n8. Once your instance has been restarted, confirm your changes under\n **Database flags** on the **Overview** page.\n\n### Create a publication and a replication slot\n\n1. Connect to the database as a user with sufficient privileges to create a replication slot; otherwise, run the following command:\n\n ```\n ALTER USER USER_NAME WITH REPLICATION;\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: The name of the user to whom you want to grant replication privileges.\n\n | If your current user can't run the command, then reconnect to the database with the default `postgres` username and run the command.\n2. Create a publication. We recommend that you create a publication only for the\n tables that you want to replicate. This allows Datastream to read-only\n the relevant data, and lowers the load on the database and Datastream:\n\n ```\n CREATE PUBLICATION PUBLICATION_NAME\n FOR TABLE SCHEMA1.TABLE1, SCHEMA2.TABLE2;\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePUBLICATION_NAME\u003c/var\u003e: The name of your publication. You'll need to provide this name when you create a stream in the Datastream stream creation wizard.\n - \u003cvar translate=\"no\"\u003eSCHEMA\u003c/var\u003e: The name of the schema that contains the table.\n - \u003cvar translate=\"no\"\u003eTABLE\u003c/var\u003e: The name of the table that you want to replicate.\n\n You can also create a publication for all tables in your database. Note that\n this approach increases the load on both the source database and Datastream: \n\n ```\n CREATE PUBLICATION PUBLICATION_NAME FOR ALL TABLES;\n ```\n | For PostgreSQL 15 and later, you can create a publication for all tables in a schema. This approach lets you replicate changes for tables in the specified list of schemas, including tables that you create in the future: \n |\n | ```\n | CREATE PUBLICATION PUBLICATION_NAME\n | FOR TABLES IN SCHEMA SCHEMA1, SCHEMA2;\n | ```\n3. Create a replication slot by entering the following SQL command:\n\n ```\n SELECT PG_CREATE_LOGICAL_REPLICATION_SLOT('REPLICATION_SLOT_NAME', 'pgoutput');\n ```\n\n \u003cbr /\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eREPLICATION_SLOT_NAME\u003c/var\u003e: The name of your replication slot. You'll need to provide this name when you create a stream in the Datastream stream creation wizard.\n\n | The replication slot name must be unique for each stream that's replicating from this database.\n\n### Create a Datastream user\n\n1. Connect to the database using a PostgreSQL client.\n\n2. Enter the following PostgreSQL command:\n\n ```\n CREATE USER USER_NAME WITH REPLICATION LOGIN PASSWORD 'USER_PASSWORD';\n ```\n\n \u003cbr /\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: The name of the Datastream user that you want to create.\n - \u003cvar translate=\"no\"\u003eUSER_PASSWORD\u003c/var\u003e: The login password for the Datastream user that you want to create.\n3. Grant the following privileges to the user you created:\n\n ```\n GRANT SELECT ON ALL TABLES IN SCHEMA SCHEMA_NAME TO USER_NAME;\n GRANT USAGE ON SCHEMA SCHEMA_NAME TO USER_NAME;\n ALTER DEFAULT PRIVILEGES IN SCHEMA SCHEMA_NAME\n GRANT SELECT ON TABLES TO USER_NAME;\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSCHEMA_NAME\u003c/var\u003e: The name of the schema to which you want to grant the privileges.\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: The user to whom you want to grant the privileges.\n\nWhat's next\n-----------\n\n- Learn more about how Datastream works with [PostgreSQL sources](/datastream/docs/sources-postgresql)."]]