> ## 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.

# Quickstart for Managed Postgres

> Experience NVMe-powered Postgres performance and add real-time analytics with native ClickHouse integration

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>;
};

ClickHouse Managed Postgres is enterprise-grade Postgres backed by NVMe storage, delivering up to 10x faster performance for disk-bound workloads compared to network-attached storage like EBS. This quickstart is divided into two parts:

* **Part 1:** Get started with NVMe Postgres and experience its performance
* **Part 2:** Unlock real-time analytics by integrating with ClickHouse

Managed Postgres is currently available on AWS in several regions and is free during private preview.

**In this quickstart, you will:**

* Create a Managed Postgres instance with NVMe-powered performance
* Load 1 million sample events and see NVMe speed in action
* Run queries and experience low-latency performance
* Replicate data to ClickHouse for real-time analytics
* Query ClickHouse directly from Postgres using `pg_clickhouse`

<h2 id="part-1">
  Part 1: Get Started with NVMe Postgres
</h2>

<h3 id="create-postgres-database">
  Create a database
</h3>

To create a new Managed Postgres service, click on the **New service** button in the service list of the Cloud Console. You should then be able to select Postgres as the database type.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/create-service.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=e2ce0f1870fad32c206e69d0d511f7ef" alt="Create a managed Postgres service" size="md" border width="1666" height="1634" data-path="images/managed-postgres/create-service.png" />

Enter a name for your database instance and click on **Create service**. You will be taken to the overview page.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/overview.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=60c99fdf9cb5b5f9a0d3b39579b6d642" alt="Managed Postgres overview" size="md" border width="3680" height="2324" data-path="images/managed-postgres/overview.png" />

Your Managed Postgres instance will be provisioned and ready for use in 3-5 minutes.

<h3 id="connect">
  Connect to your database
</h3>

In the sidebar on the left, you will see a [**Connect** button](/products/managed-postgres/connection). Click on it to view your connection details and connection strings in multiple formats.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/connect-modal.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=dd99b70c6babef97cbeadc62972592b8" alt="Managed Postgres connect modal" size="md" border width="1910" height="1728" data-path="images/managed-postgres/connect-modal.png" />

Copy the `psql` connection string and connect to your database. You can also use any Postgres-compatible client such as DBeaver, or any application library.

<h3 id="nvme-performance">
  Experience NVMe performance
</h3>

Let's see NVMe-powered performance in action. First, enable timing in psql to measure query execution:

```sql theme={null}
\timing
```

Create two sample tables for events and users:

```sql theme={null}
CREATE TABLE events (
   event_id SERIAL PRIMARY KEY,
   event_name VARCHAR(255) NOT NULL,
   event_type VARCHAR(100),
   event_timestamp TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
   event_data JSONB,
   user_id INT,
   user_ip INET,
   is_active BOOLEAN DEFAULT TRUE,
   created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
   updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE users (
   user_id SERIAL PRIMARY KEY,
   name VARCHAR(100),
   country VARCHAR(50),
   platform VARCHAR(50)
);
```

Now, insert 1 million events and watch the NVMe speed:

```sql theme={null}
INSERT INTO events (event_name, event_type, event_timestamp, event_data, user_id, user_ip)
SELECT
   'Event ' || gs::text AS event_name,
   CASE
       WHEN random() < 0.5 THEN 'click'
       WHEN random() < 0.75 THEN 'view'
       WHEN random() < 0.9 THEN 'purchase'
       WHEN random() < 0.98 THEN 'signup'
       ELSE 'logout'
   END AS event_type,
   NOW() - INTERVAL '1 day' * (gs % 365) AS event_timestamp,
   jsonb_build_object('key', 'value' || gs::text, 'additional_info', 'info_' || (gs % 100)::text) AS event_data,
   GREATEST(1, LEAST(1000, FLOOR(POWER(random(), 2) * 1000) + 1)) AS user_id,
   ('192.168.1.' || ((gs % 254) + 1))::inet AS user_ip
FROM
   generate_series(1, 1000000) gs;
```

```text theme={null}
INSERT 0 1000000
Time: 3596.542 ms (00:03.597)
```

<Tip>
  **NVMe Performance**

  1 million rows with JSONB data inserted in under 4 seconds. On traditional cloud databases using network-attached storage like EBS, this same operation typically takes 2-3x longer due to network round-trip latency and IOPS throttling. NVMe storage eliminates these bottlenecks by keeping storage physically attached to the compute.

  Performance varies based on instance size, current load, and data characteristics.
</Tip>

Insert 1,000 users:

```sql theme={null}
INSERT INTO users (name, country, platform)
SELECT
    first_names[first_idx] || ' ' || last_names[last_idx] AS name,
    CASE
        WHEN random() < 0.25 THEN 'India'
        WHEN random() < 0.5 THEN 'USA'
        WHEN random() < 0.7 THEN 'Germany'
        WHEN random() < 0.85 THEN 'China'
        ELSE 'Other'
    END AS country,
    CASE
        WHEN random() < 0.2 THEN 'iOS'
        WHEN random() < 0.4 THEN 'Android'
        WHEN random() < 0.6 THEN 'Web'
        WHEN random() < 0.75 THEN 'Windows'
        WHEN random() < 0.9 THEN 'MacOS'
        ELSE 'Linux'
    END AS platform
FROM
    generate_series(1, 1000) AS seq
    CROSS JOIN LATERAL (
        SELECT
            array['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank', 'Grace', 'Hank', 'Ivy', 'Jack', 'Liam', 'Olivia', 'Noah', 'Emma', 'Sophia', 'Benjamin', 'Isabella', 'Lucas', 'Mia', 'Amelia', 'Aarav', 'Riya', 'Arjun', 'Ananya', 'Wei', 'Li', 'Huan', 'Mei', 'Hans', 'Klaus', 'Greta', 'Sofia'] AS first_names,
            array['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis', 'Martinez', 'Taylor', 'Anderson', 'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Moore', 'Lee', 'Perez', 'Sharma', 'Patel', 'Gupta', 'Reddy', 'Zhang', 'Wang', 'Chen', 'Liu', 'Schmidt', 'Müller', 'Weber', 'Fischer'] AS last_names,
            1 + (seq % 32) AS first_idx,
            1 + ((seq / 32)::int % 32) AS last_idx
    ) AS names;
```

<h3 id="run-queries">
  Run queries on your data
</h3>

Now let's run some queries to see how fast Postgres responds with NVMe storage.

**Aggregate 1 million events by type:**

```sql theme={null}
SELECT event_type, COUNT(*) as count 
FROM events 
GROUP BY event_type 
ORDER BY count DESC;
```

```text theme={null}
 event_type | count  
------------+--------
 click      | 499523
 view       | 375644
 purchase   | 112473
 signup     |  12117
 logout     |    243
(5 rows)

Time: 114.883 ms
```

**Query with JSONB filtering and date range:**

```sql theme={null}
SELECT COUNT(*) 
FROM events 
WHERE event_timestamp > NOW() - INTERVAL '30 days'
  AND event_data->>'additional_info' LIKE 'info_5%';
```

```text theme={null}
 count 
-------
  9042
(1 row)

Time: 109.294 ms
```

**Join events with users:**

```sql theme={null}
SELECT u.country, COUNT(*) as events, AVG(LENGTH(e.event_data::text))::int as avg_json_size
FROM events e
JOIN users u ON e.user_id = u.user_id
GROUP BY u.country
ORDER BY events DESC;
```

```text theme={null}
 country | events | avg_json_size 
---------+--------+---------------
 USA     | 383748 |            52
 India   | 255990 |            52
 Germany | 223781 |            52
 China   | 127754 |            52
 Other   |   8727 |            52
(5 rows)

Time: 224.670 ms
```

<Info>
  **Your Postgres is ready**

  At this point, you have a fully functional, high-performance Postgres database ready for your transactional workloads.

  Continue to Part 2 to see how native ClickHouse integration can supercharge your analytics.
</Info>

***

<h2 id="part-2">
  Part 2: Add Real-Time Analytics with ClickHouse
</h2>

While Postgres excels at transactional workloads (OLTP), ClickHouse is purpose-built for analytical queries (OLAP) on large datasets. By integrating the two, you get the best of both worlds:

* **Postgres** for your application's transactional data (inserts, updates, point lookups)
* **ClickHouse** for sub-second analytics on billions of rows

This section shows you how to replicate your Postgres data to ClickHouse and query it seamlessly.

<h3 id="setup-integrate-clickhouse">
  Setup ClickHouse integration
</h3>

Now that we have tables and data in Postgres, let's replicate the tables to ClickHouse for analytics. We start by clicking on **ClickHouse integration** in the sidebar. Then you can click on **Replicate data in ClickHouse**.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/integration-landing.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=be942375ed2b8d7dd5381c5422be2024" alt="Managed Postgres integration empty" size="md" border width="3448" height="1982" data-path="images/managed-postgres/integration-landing.png" />

In the form that follows, you can enter a name for your integration and select an existing ClickHouse instance to replicate to. If you don't have a ClickHouse instance yet, you can create one directly from this form.

<Info>
  **Important**

  Make sure the ClickHouse service you select is Running before proceeding.
</Info>

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/postgres-analytics-form.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=bd2947cea7fdeab9e61fb4f133412fe6" alt="Managed Postgres integration form" size="md" border width="3400" height="1976" data-path="images/managed-postgres/postgres-analytics-form.png" />

Click on **Next**, to be taken to the table picker. Here all you need to do is:

* Select a ClickHouse database to replicate to.
* Expand the **public** schema and select the users and events table we created earlier.
* Click on **Replicate data to ClickHouse**.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/table-picker.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=2dd23428c2ce1b72cd525c7d79243930" alt="Managed Postgres table picker" size="md" border width="3400" height="1976" data-path="images/managed-postgres/table-picker.png" />

The replication process will start, and you will be taken to the integration overview page. Being the first integration, it can take 2-3 minutes to setup the initial infrastructure. In the meantime let's check out the new **pg\_clickhouse** extension.

<h3 id="pg-clickhouse-extension">
  Query ClickHouse from Postgres
</h3>

The `pg_clickhouse` extension lets you query ClickHouse data directly from Postgres using standard SQL. This means your application can use Postgres as a unified query layer for both transactional and analytical data. See the [full documentation](/products/managed-postgres/extensions/pg_clickhouse/introduction) for details.

Enable the extension:

```sql theme={null}
CREATE EXTENSION pg_clickhouse;
```

Then, create a foreign server connection to ClickHouse. Use the `http` driver with port `8443` for secure connections:

```sql theme={null}
CREATE SERVER ch FOREIGN DATA WRAPPER clickhouse_fdw
       OPTIONS(driver 'http', host '<clickhouse_cloud_host>', dbname '<database_name>', port '8443');
```

Replace `<clickhouse_cloud_host>` with your ClickHouse hostname and `<database_name>` with the database you selected during replication setup. You can find the hostname in your ClickHouse service by clicking **Connect** in the sidebar.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/get-clickhouse-host.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=4b4d81adb4ef6b2b7db738ac90fa3047" alt="Get ClickHouse host" size="md" border width="695" height="765" data-path="images/managed-postgres/get-clickhouse-host.png" />

Now, we map the Postgres user to the ClickHouse service's credentials:

```sql theme={null}
CREATE USER MAPPING FOR CURRENT_USER SERVER ch 
OPTIONS (user 'default', password '<clickhouse_password>');
```

Now import the ClickHouse tables into a Postgres schema:

```sql theme={null}
CREATE SCHEMA organization;
IMPORT FOREIGN SCHEMA "<database_name>" FROM SERVER ch INTO organization;
```

Replace `<database_name>` with the same database name you used when creating the server.

You can now see all the ClickHouse tables in your Postgres client:

```sql theme={null}
\det+ organization.*
```

<h3 id="analytics-after-integration">
  See your analytics in action
</h3>

Let's check back on the integration page. You should see that the initial replication is complete. Click on the integration name to view details.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/analytics-list.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=978a4d46bbe87f748748d4508f9f411d" alt="Managed Postgres analytics list" size="md" border width="1821" height="319" data-path="images/managed-postgres/analytics-list.png" />

Click on the service name to open the ClickHouse console and see your replicated tables.

<Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/qT0j4CNmQubVqREl/images/managed-postgres/replicated-tables.png?fit=max&auto=format&n=qT0j4CNmQubVqREl&q=85&s=cb1988bd918f0a7acda87db26449a5df" alt="Managed Postgres replicated tables in ClickHouse" size="md" border width="1725" height="1179" data-path="images/managed-postgres/replicated-tables.png" />

<h3 id="performance-comparison">
  Compare Postgres vs ClickHouse performance
</h3>

Now let's run some analytical queries and compare performance between Postgres and ClickHouse. Note that replicated tables use the naming convention `public_<table_name>`.

**Query 1: Top users by activity**

This query finds the most active users with multiple aggregations:

```sql theme={null}
-- Via ClickHouse
SELECT 
    user_id,
    COUNT(*) as total_events,
    COUNT(DISTINCT event_type) as unique_event_types,
    SUM(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) as purchases,
    MIN(event_timestamp) as first_event,
    MAX(event_timestamp) as last_event
FROM organization.public_events
GROUP BY user_id
ORDER BY total_events DESC
LIMIT 10;
```

```text theme={null}
 user_id | total_events | unique_event_types | purchases |        first_event         |         last_event         
---------+--------------+--------------------+-----------+----------------------------+----------------------------
       1 |        31439 |                  5 |      3551 | 2025-01-22 22:40:45.612281 | 2026-01-21 22:40:45.612281
       2 |        13235 |                  4 |      1492 | 2025-01-22 22:40:45.612281 | 2026-01-21 22:40:45.612281
...
(10 rows)

Time: 163.898 ms   -- ClickHouse
Time: 554.621 ms   -- Same query on Postgres
```

**Query 2: User engagement by country and platform**

This query joins events with users and computes engagement metrics:

```sql theme={null}
-- Via ClickHouse
SELECT 
    u.country,
    u.platform,
    COUNT(DISTINCT e.user_id) as users,
    COUNT(*) as total_events,
    ROUND(COUNT(*)::numeric / COUNT(DISTINCT e.user_id), 2) as events_per_user,
    SUM(CASE WHEN e.event_type = 'purchase' THEN 1 ELSE 0 END) as purchases
FROM organization.public_events e
JOIN organization.public_users u ON e.user_id = u.user_id
GROUP BY u.country, u.platform
ORDER BY total_events DESC
LIMIT 10;
```

```text theme={null}
 country | platform | users | total_events | events_per_user | purchases 
---------+----------+-------+--------------+-----------------+-----------
 USA     | Android  |   115 |       109977 |             956 |     12388
 USA     | Web      |   108 |       105057 |             972 |     11847
 USA     | iOS      |    83 |        84594 |            1019 |      9565
 Germany | Android  |    85 |        77966 |             917 |      8852
 India   | Android  |    80 |        68095 |             851 |      7724
...
(10 rows)

Time: 170.353 ms   -- ClickHouse
Time: 1245.560 ms  -- Same query on Postgres
```

**Performance comparison:**

| Query                                 | Postgres (NVMe) | ClickHouse (via pg\_clickhouse) | Speedup |
| ------------------------------------- | --------------- | ------------------------------- | ------- |
| Top users (5 aggregations)            | 555 ms          | 164 ms                          | 3.4x    |
| User engagement (JOIN + aggregations) | 1,246 ms        | 170 ms                          | 7.3x    |

<Tip>
  **When to use ClickHouse**

  Even on this 1M row dataset, ClickHouse delivers 3-7x faster performance on complex analytical queries with JOINs and multiple aggregations. The difference becomes even more dramatic at larger scales (100M+ rows), where ClickHouse's columnar storage and vectorized execution can deliver 10-100x speedups.

  Query times vary based on instance size, network latency between services, data characteristics, and current load.
</Tip>

<h2 id="cleanup">
  Cleanup
</h2>

To delete the resources created in this quickstart:

1. First, delete the ClickPipe integration from the ClickHouse service
2. Then, delete the Managed Postgres instance from the Cloud Console
