> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrate to Managed Postgres using pg_dump and pg_restore

> Learn how to migrate your PostgreSQL data to ClickHouse Managed Postgres using pg_dump and pg_restore

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

export const galaxyOnClick = eventName => () => {
  try {
    if (typeof window !== "undefined" && window.galaxy && eventName) {
      window.galaxy.track(eventName, {
        interaction: "click"
      });
    }
  } catch (e) {}
};

export const BetaBadge = ({link, galaxyTrack, galaxyEvent}) => {
  if (link) {
    return <a href={link} target="_blank" rel="noopener noreferrer" className="betaBadge" onClick={galaxyTrack && galaxyEvent ? galaxyOnClick(galaxyEvent) : undefined}>
                <Icon />
                <span>Beta</span>
            </a>;
  }
  return <div className="betaBadge">
            <Icon />
            <span>
                Beta feature. 
                <u>
                    <a href="/docs/beta-and-experimental-features#beta-features">
                        Learn more.
                    </a>
                </u>
            </span>
        </div>;
};

This guide provides step-by-step instructions on how to migrate your PostgreSQL database to ClickHouse Managed Postgres using the `pg_dump` and `pg_restore` utilities.

<h2 id="migration-pgdump-pg-restore-prerequisites">
  Prerequisites
</h2>

* Access to your source PostgreSQL database.
* `pg_dump` and `pg_restore` installed on your local machine. These are typically included with PostgreSQL installations. If not, you can download them from the [PostgreSQL official website](https://www.postgresql.org/download/).

<h2 id="migration-pgdump-pg-restore-setup">
  The setup
</h2>

To go through the steps, let's use a sample RDS Postgres database as the source database. Something like this:

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/pg_dump_restore/source-setup.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=172023020275391d1773c80d096a1969" alt="Source PostgreSQL Database Setup" size="xl" border width="3766" height="2312" data-path="images/managed-postgres/pg_dump_restore/source-setup.png" />

Here's what we're working with:

* Two tables - `events` and `users`. `events` has a million rows, and `users` has a thousand rows.
* `events` has an index.
* A view on top of the `events` table.
* Couple of sequences

<h2 id="migration-pgdump-pg-restore-dump">
  Create a dump of the source database
</h2>

Now let's use `pg_dump` to create a dump file of the above objects. It's a simple command:

```shell theme={null}
pg_dump \
  -d 'postgresql://<user>:<password>@<host>:<port>/<database>' \
  --format directory \
  -f rds-dump
```

Here:

* Replace `<user>`, `<password>`, `<host>`, `<port>`, and `<database>` with your source database credentials. Most Postgres providers give you a connection string that you can use directly.
* `--format directory` specifies that we want the dump in a directory format, which is suitable for `pg_restore`.
* `-f rds-dump` specifies the output directory for the dump files. Note that this directory will be created automatically and shouldn't exist beforehand.
* You can also parallelize the dump process by adding the `--jobs` flag followed by the number of parallel jobs you want to run. For more details, refer to the [pg\_dump documentation](https://www.postgresql.org/docs/current/app-pgdump.html).

<Tip>
  You can test this process once to get a sense of how long it takes and the size of the dump file.
</Tip>

Here's what running this command looks like:

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/pg_dump_restore/dump-command.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=ead214a4f46d3f6db79bd9ccf9e51e41" alt="pg_dump Command Execution" size="xl" border width="2158" height="386" data-path="images/managed-postgres/pg_dump_restore/dump-command.png" />

<h2 id="migration-pgdump-pg-restore-restore">
  Migrate the dump to ClickHouse Managed Postgres
</h2>

Now that we have the dump file, we can restore it to our ClickHouse Managed Postgres instance using `pg_restore`.

<h3 id="migration-pgdump-pg-restore-create-pg">
  Create a Managed Postgres instance
</h3>

First, ensure you have a Managed Postgres instance set up, preferably in the same region as the source. You can follow the quick guide [here](/products/managed-postgres/quickstart#create-postgres-database). Here's what we're going to spin up for this guide:

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/pg_dump_restore/create-pg-for-migration.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=966d2a5124a3a3802e8c1072112c7e9b" alt="Create ClickHouse Managed Postgres Instance" size="md" border width="1532" height="1570" data-path="images/managed-postgres/pg_dump_restore/create-pg-for-migration.png" />

<h3 id="migration-pgdump-pg-restore-restore-dump">
  Restore the dump
</h3>

Now, heading back to our local machine, we can use the `pg_restore` command to restore the dump to our Managed Postgres instance:

```shell theme={null}
pg_restore \
  -d 'postgresql://<user>:<password>@<pg_clickhouse_host>:5432/<database>' \
  --verbose \
  rds-dump
```

You can get the connection string for your Managed Postgres instance from the ClickHouse Cloud console, explained very simply [here](/products/managed-postgres/connection).

Here too there are a couple of flags to note:

* `--verbose` provides detailed output during the restore process.
* You can also use the `--jobs` flag here to parallelize the restore process. For more details, refer to the [pg\_restore documentation](https://www.postgresql.org/docs/current/app-pgrestore.html).

In our case, it looks like this:

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/pg_dump_restore/restore-command.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=67506ceea8f40517048a77a18629152f" alt="pg_restore Command Execution" size="xl" border width="2828" height="938" data-path="images/managed-postgres/pg_dump_restore/restore-command.png" />

<h2 id="migration-pgdump-pg-restore-verify">
  Verify the migration
</h2>

Once the restore process is complete, you can connect to your Managed Postgres instance and verify that all your data and objects have been migrated successfully. You can use any PostgreSQL client to connect and run queries.
Here's what our Managed Postgres setup looks like after the migration:

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/pg_dump_restore/target-setup.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=b9131e0cb02df6e8300f7e17d70fc7d5" alt="Target Managed Postgres Database Setup" size="xl" border width="3330" height="1966" data-path="images/managed-postgres/pg_dump_restore/target-setup.png" />

We see that we have all our tables, indexes, views, and sequences intact, along with the data counts matching.

<h2 id="migration-pgdump-pg-restore-considerations">
  Considerations
</h2>

* Ensure that the PostgreSQL versions of the source and target databases are compatible.
  Using a pg\_dump version older than the source server may lead to missing features or restore issues. Ideally, use the same or newer major version of pg\_dump than the source database.
* Large databases may take a significant amount of time to dump and restore.
  Plan accordingly to minimize downtime, and consider using parallel dumps/restores (--jobs) where supported.
* Note that pg\_dump / pg\_restore don't replicate all database-related objects or runtime state.
  These include roles and role memberships, replication slots, server-level configuration (e.g. postgresql.conf, pg\_hba.conf), tablespaces, and runtime statistics.

<h2 id="migration-pgdump-pg-restore-next-steps">
  Next steps
</h2>

Congratulations! You have successfully migrated your PostgreSQL database to ClickHouse Managed Postgres using pg\_dump and pg\_restore. You're now all set to explore Managed Postgres features and its integration with ClickHouse. Here's a 10 minute quickstart to get you going:

* [Managed Postgres Quickstart Guide](/products/managed-postgres/quickstart)
