Export and import using pg_dump, pg_dumpall, and pg_restore Stay organized with collections Save and categorize content based on your preferences.
This page describes exporting and importing data into Cloud SQL instances usingpg_dump, pg_dumpall, and pg_restore.
Note: If you're migrating an entire database froma supported database server (on-premises, in AWS, or Cloud SQL) to a newCloud SQL instance, you can use theDatabase Migration Service instead of exportingand then importing files.If you're exporting because you want to create a new instance from the exportedfile, considerrestoring from a backup to a different instanceorcloning the instance.Before you begin
Important: Before starting a large export, ensure that at least 25 percentof the database size is free (on the instance). Doing so helps preventissues with aggressive autogrowth, which can affect the availabilityof the instance.Exports use database resources, but they do not interfere with normal databaseoperations unless the instance is under-provisioned.
For best practices, seeBest Practices for Importing and Exporting Data.
After completing an import operation,verify theresults.
Learn more about thepg_dump
,pg_dumpall
, andpg_restore
utilities.
Export data from Cloud SQL for PostgreSQL
You can use Cloud SQL to perform an export from the Google Cloud console,thegcloud CLI, or the API.
- To export a single PostgreSQL database, use the
pg_dump
utility. - To export all PostgreSQL databases of a cluster, use the
pg_dumpall
utility.
When using either utility, make sure that you also use the required options toensure that the resulting export file is valid for import back into Cloud SQL.
Export data from an on-premises PostgreSQL server using pg_dump
To export a database that is not managed by Cloud SQL, for later import into Cloud SQL, use thepg_dump
utility with the following flags:
--no-owner
Ownership change commands must not be included in the dump file.
--format
The
custom
anddirectory
formats are allowed if the dump file is intended for use withpg_restore
.For
plain-text
format, export to aSQL dump file
instead. This format is not compatible withpg_restore
, and must be imported using the Google Cloud console import command orpsql
client.--no-acl
This flag is required if your dump would otherwise contain statements to grant or revoke membership in a
SUPERUSER
role.--clean
This optional flag enables you to include the
DROP <object>
SQL statement that's required to drop (clean) database objects before you import them.--if-exists
This optional flag enables you to include the
IF EXISTS
SQL statement with eachDROP
statement that's produced by theclean
flag.
In addition, you must remove all of the following:
- Extension-related statements, if Cloud SQL does not support that extension. SeePostgreSQL Extensions for the list of supported extensions.
CREATE EXTENSION
orDROP EXTENSION
statements referencing plpgsql. This extension comes pre-installed on Cloud SQL Postgres instances.COMMENT ON EXTENSION
statements.
Confirm that the default encoding, as determined by the database settings, is correct for your data. If needed, you can override the default with the--encoding
flag.
Export data using thecustom
format from Cloud SQL for PostgreSQL
To use the custom format, from a command line, runpg_dump
:
pg_dump\-UUSERNAME\--format=custom\--no-owner\--no-acl\DATABASE_NAME>DATABASE_NAME.dmp
Export data from multiple files in parallel from Cloud SQL for PostgreSQL
You can only use thedirectory
output format toexport data from multiple files in parallel.
To export in parallel, use the-jNUM_CORES
flag.NUM_CORES is the number of cores on the source instance.
Export all databases
pg_dumpall
isa utility that allows you to extract all PostgreSQL databases of a cluster intoa single script file. This file has SQL commands that you can use to restore thedatabases.
To export all PostgreSQL databases in a Cloud SQL instance, use thepg_dumpall
utility with the following mandatory flags:
exclude-database=cloudsqladmin
exclude-database=template*
Thepg_dumpall
utility doesn't have access to thecloudsqladmin
ortemplate
databases.
pg_dumpall
utility to export all PostgreSQL databases of a cluster that aren't managed by Cloud SQL, then you don't have to use theexclude-database=cloudsqladmin
flag.If an instance has multiple databases and these databases have multiple owners, then this command fails. If this occurs, then use thepg_dump
utility to export the databases individually.To export all PostgreSQL databases, run the following command:
pg_dumpall\-hHOST_NAME-lDATABASE_NAME--exclude-database=cloudsqladmin\--exclude-database=template*>pg_dumpall.sql
To view role passwords when dumping roles withpg_dumpall
,set thecloudsql.pg_authid_select_role
flag to a PostgreSQL role name. If the role exists,then it has read-only (SELECT
) access to thepg_authid
table. This tablecontains role passwords.
Import
Use thepg_restore
utility to import an archive into aCloud SQL database.pg_restore
works only with archivescreated bypg_dump in either thecustom
ordirectory
formats.Learn more aboutpg_restore
.
Import from a dump file created with thecustom
format to Cloud SQL for PostgreSQL
If the dump file was created with custom format, run the following command:
pg_restore\--listDATABASE_NAME.dmp|sed-E's/(.* EXTENSION )/; \1/g'>DATABASE_NAME.toc
Post-processing fromsed
comments out all extension statements in the SQL dump file.
When importing usingpg_restore
, specify the processed table of contents withthe command-line argument "--use-list=DATABASE_NAME.toc".
Import data from multiple files in parallel to Cloud SQL for PostgreSQL
You canimport data from multiple files in parallel only for archives created using thedirectory
andcustom
output formats.
To import in parallel, use the-jNUM_CORES
flag.NUM_CORES is the number of cores on the destination instance.
Import performance on Cloud SQL for PostgreSQL
pg_restore
performance depends on the writeperformance of the Cloud SQL instance.Learn more about performance in Cloud SQL.What's next
- Learn how tocheck the status of import and export operations.
- Learn more aboutbest practices for importing and exporting data.
- Learn more about thePostgreSQL pg_dump utility.
- Known issues for imports and exports.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-14 UTC.