Extensions
Running SQL Queries in PostgreSQL
#### The `postgres_query` Table Function {#docs:extensions:postgres::the-postgres_query-table-function}  
The `postgres_query` table function allows you to run arbitrary read queries within an attached database. `postgres_query` takes the name of the attached PostgreSQL database to execute the query in, as well as the SQL query to execute. The result of the query is returned. Single-quote strings are escaped by repeating the single quote twice.  
```sql
postgres_query(attached_database::VARCHAR, query::VARCHAR)
```  
For example:  
```sql
ATTACH 'dbname=postgresscanner' AS postgres_db (TYPE POSTGRES);
SELECT * FROM postgres_query('postgres_db', 'SELECT * FROM cars LIMIT 3');
```  
<!--
CREATE TABLE cars (brand VARCHAR, model VARCHAR, color VARCHAR);
INSERT INTO cars VALUES
('Ferrari', 'Testarossa', 'red'),
('Aston Martin', 'DB2', 'blue'),
('Bentley', 'Mulsanne', 'gray')
;
-->  
| brand        | model      | color |
| ------------ | ---------- | ----- |
| Ferrari      | Testarossa | red   |
| Aston Martin | DB2        | blue  |
| Bentley      | Mulsanne   | gray  |  
#### The `postgres_execute` Function {#docs:extensions:postgres::the-postgres_execute-function}  
The `postgres_execute` function allows running arbitrary queries within PostgreSQL, including statements that update the schema and content of the database.  
```sql
ATTACH 'dbname=postgresscanner' AS postgres_db (TYPE POSTGRES);
CALL postgres_execute('postgres_db', 'CREATE TABLE my_table (i INTEGER)');
```