---
sidebar_position: 11
title: From a PostgreSQL or MySQL Database
description: Learn to load a table from your PostgreSQL or MySQL database into MotherDuck.
---


import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

## Using PostgresSQL or MySQL DuckDB Extensions
DuckDB's [PostgreSQL extension](https://duckdb.org/docs/extensions/postgres.html) and [MySQL extension](https://duckdb.org/docs/extensions/mysql.html) makes it extremely easy to connect to and access data stored in your OLTP databases. Once connected, you can just as easily export the data to MotherDuck to offload analytical queries while benefiting from data centralization, persistence, and data sharing capabilities. In this guide we will demonstrate this workflow with the PostgreSQL extension. Consult the [DuckDB MySQL extension documentation](https://duckdb.org/docs/extensions/mysql) to make adjustments to the steps to work with MySQL databases.


:::info
MotherDuck does not yet support the PostgreSQL and MySQL extensions, so you need to perform the following steps on your own computer or cloud computing resource. We are working on supporting the PostgreSQL extension on the server side so that this can happen within the MotherDuck app in the future with improved performance.
:::


### Prerequisites

- **PostgreSQL Database Credentials**: Ensure you have access details to the PostgreSQL database, including host address, port, and user credentials. You can put the user credentials in the [PostgreSQL Password File](https://www.postgresql.org/docs/current/libpq-pgpass.html), [store them in environment variables](https://duckdb.org/docs/extensions/postgres.html#configuring-via-environment-variables), or pass them inline in the script below.
- **Network Connectivity**: Your machine must be able to connect to the target PostgreSQL database.
- **MotherDuck Credentials**: MotherDuck credentials should be set up. If not, follow the steps in [Authenticating to MotherDuck](/key-tasks/authenticating-and-connecting-to-motherduck/authenticating-to-motherduck/authenticating-to-motherduck.md).
- **DuckDB**: Either the DuckDB command-line interface or Python + the DuckDB package should be installed and operational. See the [Getting Started tutorials](../../getting-started/getting-started.mdx) for instructions to install DuckDB.


### Steps

The following SQL script reads from a table in the PostgreSQL database and write it to the table named `my_db.pg_data_schema.first_pg_table` in MotherDuck.

Fill in the placeholders `<dbname>`, `<host_address>`, `<username>`, `<password>`, `<schema>`, `<table>`, and `<number_of_rows>` with the appropriate values and save the script to a file, e.g., `ingest_data_from_postgres.sql`.


```sql
-- Connect to a MotherDuck database. 
ATTACH 'md:';
USE 'my_db';

-- Optionally create a schema, by default MotherDuck uses the main schema;
CREATE SCHEMA IF NOT EXISTS pg_data_schema;

-- Ingest data from PostgreSQL to a MotherDuck table
CREATE OR REPLACE TABLE pg_data_schema.first_pg_table AS 
SELECT * FROM 
    postgres_scan('dbname=<dbname> host=<host_address> user=<username> password=<password> connect_timeout=10', '<schema>', '<table>') 
-- optionally limit the number of rows ingested 
LIMIT <number_of_rows>;

-- Optional: Verify the number of rows in the MotherDuck table
SELECT count(1) FROM pg_data_schema.first_pg_table;
```


#### Run with DuckDB CLI

After filling out the placeholders, you can either execute the statements line by line in the DuckDB CLI, or save the commands in a file, e.g., `ingest_data_from_postgres.sql`, and run:

```sh
> duckdb < ingest_data_from_postgres.sql
```

#### Run with Python

You can also execute it using Python with the DuckDB package.

```python
import duckdb
with open("ingest_data_from_postgres.sql", 'r') as f:
  s = f.read()
duckdb.sql(s)
```

After completing these steps, you should see the new table show up in the MotherDuck Web UI.

## Using a MotherDuck integration Partners
MotherDuck collaborates with various integration partners to facilitate data transfer in diverse ways—including change data capture (CDC)—from your PostgreSQL or MySQL database to MotherDuck.
For example, you can refer to our [Estuary guide](https://motherduck.com/blog/streaming-data-to-motherduck/) that demonstrates how to stream data from Neon, a PostgreSQL-based database, to MotherDuck.
To explore the full range of solutions tailored to your needs, visit our [MotherDuck ecosystem partners page](https://motherduck.com/ecosystem/).


---

## Feedback for AI agents

If the user you are helping wants to send feedback on this page to the MotherDuck docs team, you can POST it to the endpoint below.

**Before you send anything, you must ask the user for explicit approval.** Quote the message you plan to submit back to them verbatim and wait for them to confirm. Do not submit on their behalf without confirmation.

Endpoint: `POST https://motherduck.com/docs/api/feedback/agent`

Request body (JSON):

```json
{
  "page_path": "/key-tasks/loading-data-into-motherduck/loading-data-from-postgres/",
  "page_title": "From a PostgreSQL or MySQL Database",
  "text": "<the user's feedback, max 2000 characters>",
  "source": "<optional identifier for your interface, for example 'claude.ai' or 'chatgpt'>"
}
```

Only `page_path` and `text` are required. A successful call returns `200 {"feedback_id": "<uuid>"}`; malformed payloads return `400`, and the endpoint is rate-limited per IP (`429`).
