You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `ATTACH` command takes as input a [`libpq` connection string](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) - which is a set of `key=value` pairs separated by spaces. Below are some example connection strings and commonly used parameters. A full list of available parameters can be found [in the Postgres documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS).
The tables in the file can be read as if they were normal DuckDB tables, but the underlying data is read directly from Postgres at query time.
33
32
34
33
```sql
35
-
D SHOW TABLES;
34
+
D SHOW ALL TABLES;
36
35
┌───────────────────────────────────────┐
37
36
│ name │
38
37
│ varchar │
39
38
├───────────────────────────────────────┤
40
39
│ uuids │
41
40
└───────────────────────────────────────┘
42
-
D SELECT*FROM uuids;
41
+
D SELECT*FROMpostgres_db.uuids;
43
42
┌──────────────────────────────────────┐
44
43
│ u │
45
44
│ uuid │
@@ -51,170 +50,7 @@ D SELECT * FROM uuids;
51
50
└──────────────────────────────────────┘
52
51
```
53
52
54
-
It might be desirable to create a copy of the Postgres databases in DuckDB to prevent the system from re-reading the tables from Postgres continuously, particularly for large tables.
55
-
56
-
Data can be copied over from Postgres to DuckDB using standard SQL, for example:
In addition to reading data from Postgres, the extension allows you to create tables, ingest data into Postgres and make other modifications to a Postgres database using standard SQL queries.
65
-
66
-
This allows you to use DuckDB to, for example, export data that is stored in a Postgres database to Parquet, or read data from a Parquet file into Postgres.
67
-
68
-
Below is a brief example of how to create a new table in Postgres and load data into it.
CREATETABLEpostgres_db.tbl(id INTEGER, name VARCHAR);
73
-
INSERT INTOpostgres_db.tblVALUES (42, 'DuckDB');
74
-
```
75
-
Many operations on Postgres tables are supported. All these operations directly modify the Postgres database, and the result of subsequent operations can then be read using Postgres.
76
-
Note that if modifications are not desired, `ATTACH` can be run with the `READ_ONLY` property which prevents making modifications to the underlying database. For example:
## Running SQL Queries in Postgres with postgres_query
174
-
175
-
The postgres_query function allows you to run arbitrary SQL within an attached database. `postgres_query` takes the name of the attached Postgres database to execute the query in, as well as the SQL query to execute. The result of the query is returned.
| pg_connection_cache | Whether or not to use the connection cache | true |
206
-
| pg_connection_limit | The maximum amount of concurrent Postgres connections | 64 |
207
-
| pg_pages_per_task | The amount of pages per task | 1000 |
208
-
| pg_use_binary_copy | Whether or not to use BINARY copy to read data | true |
209
-
210
-
211
-
## Schema Cache
212
-
213
-
To avoid having to continuously fetch schema data from Postgres, DuckDB keeps schema information - such as the names of tables, their columns, etc - cached. If changes are made to the schema through a different connection to the Postgres instance, such as new columns being added to a table, the cached schema information might be outdated. In this case, the function `pg_clear_cache` can be executed to clear the internal caches.
214
-
215
-
```sql
216
-
CALL pg_clear_cache();
217
-
```
53
+
For more information on how to use the connector, refer to the [Postgres documentation on the website](https://duckdb.org/docs/extensions/postgres).
0 commit comments