| 184 | |
| 185 | class PostgresSource(DataSource): |
| 186 | def __init__( |
| 187 | self, name: str, config: PostgresConfig, duckdb_conn: duckdb.DuckDBPyConnection |
| 188 | ): |
| 189 | super().__init__(name, duckdb_conn) |
| 190 | self.config = config |
| 191 | |
| 192 | # Initialize postgres_scanner extension |
| 193 | self._duckdb.execute("INSTALL postgres_scanner;") |
| 194 | self._duckdb.execute("LOAD postgres_scanner;") |
| 195 | |
| 196 | self._conn_string = ( |
| 197 | f"postgresql://{config.user}:{config.password}" |
| 198 | f"@{config.host}:{config.port}/{config.dbname}" |
| 199 | ) |
| 200 | |
| 201 | def query(self, sql: str) -> pd.DataFrame: |
| 202 | self._duckdb.execute(f"CALL postgres_attach('{self._conn_string}')") |