# Export Query Results To A CSV

Digging through the results of queries in Postgres's `psql` is great if you are a programmer, but eventually someone without the skills or access may need to check out that data. Exporting the results of a query to CSV is a friendly way to share said results because most people will have a program on their computer that can read a CSV file.

For example, exporting all your pokemon to `/tmp/pokemon_dump.csv` can be accomplished with:

```sql
copy (select * from pokemons) to '/tmp/pokemon_dump.csv' csv;
```

Because we are grabbing the entire table, we can just specify the table name instead of using a subquery:

```sql
copy pokemons to '/tmp/pokemon_dump.csv' csv;
```

Include the column names as headers to the CSV file with the `header` keyword:

```sql
copy (select * from pokemons) to '/tmp/pokemon_dump.csv' csv header;
```

If your user has limited access, you can use the \copy command like so:

```sql
\copy (select * from pokemons) to '/tmp/pokemon_dump.csv' with csv header;
```

[source](http://stackoverflow.com/questions/1120109/export-postgres-table-to-csv-file-with-headings)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/linux/is-app-installed/file-operations/export-query-results-to-a-csv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
