|
2 | 2 |
|
3 | 3 | ## Connect with MotherDuck |
4 | 4 |
|
5 | | -pg_duckdb also integrates with [MotherDuck][md]. To enable this support you first need to [generate an access token][md-access-token] and then add the following line to your `postgresql.conf` file: |
| 5 | +`pg_duckdb` integrates with [MotherDuck][md] natively. |
| 6 | +To enable this support you first need to [generate an access token][md-access-token]. |
| 7 | +Then you can enable it by simply using the `enable_motherduck` convenience method: |
6 | 8 |
|
7 | | -```ini |
8 | | -duckdb.motherduck_token = 'your_access_token' |
9 | | -``` |
10 | | - |
11 | | -NOTE: If you don't want to store the token in your `postgresql.conf`file can also store the token in the `motherduck_token` environment variable and then explicitly enable MotherDuck support in your `postgresql.conf` file: |
12 | | - |
13 | | -```ini |
14 | | -duckdb.motherduck_enabled = true |
| 9 | +```sql |
| 10 | +-- If not provided, the token will be read from the `motherduck_token` environment variable |
| 11 | +-- If not provided, the default MD database name is `my_db` |
| 12 | +SELECT duckdb.enable_motherduck('<optional token>', '<optional MD database name>'); |
15 | 13 | ``` |
16 | 14 |
|
17 | | -If you installed `pg_duckdb` in a different Postgres database than the default one named `postgres`, then you also need to add the following line to your `postgresql.conf` file: |
| 15 | +This convenience method creates a `motherduck` `SERVER` using the `pg_duckdb` Foreign Data Wrapper, which hosts the options for this integration. It also provides an `USER MAPPING` for the current user, which stores the provided MotherDuck token (if any). |
18 | 16 |
|
19 | | -```ini |
20 | | -duckdb.motherduck_postgres_database = 'your_database_name' |
21 | | -``` |
| 17 | +You can refer to the [section](advanced-motherduck-configuration) below for more on the `SERVER` and `USER MAPPING` configuration. |
22 | 18 |
|
23 | 19 | ### Non-supersuer configuration |
24 | 20 |
|
@@ -55,10 +51,25 @@ CREATE TABLE users_md_copy USING duckdb AS SELECT * FROM users; |
55 | 51 |
|
56 | 52 | Any tables that you already had in MotherDuck are automatically available in Postgres. Since DuckDB and MotherDuck allow accessing multiple databases from a single connection and Postgres does not, we map database+schema in DuckDB to a schema name in Postgres. |
57 | 53 |
|
58 | | -The default MotherDuck database will be easiest to use (see below for details), by default this is `my_db`. If you want to specify which MotherDuck database is your default database, then you can also add the following line to your `postgresql.conf` file: |
| 54 | +The default MotherDuck database will be easiest to use (see below for details), by default this is `my_db`. |
59 | 55 |
|
60 | | -```ini |
61 | | -duckdb.motherduck_default_database = 'your_motherduck_database_name' |
| 56 | +## Advanced MotherDuck configuration |
| 57 | + |
| 58 | +If you want to specify which MotherDuck database is your default database, then you need to configure MotherDuck using a `SERVER` and a `USER MAPPING` as such: |
| 59 | + |
| 60 | +```sql |
| 61 | +CREATE SERVER motherduck |
| 62 | +TYPE 'motherduck' |
| 63 | +FOREIGN DATA WRAPPER duckdb |
| 64 | +OPTIONS (default_database '<your database>'); |
| 65 | + |
| 66 | +-- You may use `::FROM_ENV::` to have the token be read from the environment variable |
| 67 | +CREATE USER MAPPING FOR CURRENT_USER SERVER motherduck OPTIONS (token '<your token>') |
| 68 | +``` |
| 69 | + |
| 70 | +Note: with the `duckdb.enable_motherduck` convenience method above, you can simply do: |
| 71 | +```sql |
| 72 | +SELECT duckdb.enable_motherduck('<token>', '<default database>'); |
62 | 73 | ``` |
63 | 74 |
|
64 | 75 | The mapping of database+schema to schema name is then done in the following way: |
|
0 commit comments