# 11. Import & Export Data

### Import CSV file into Table

**(*****show you how to import CSV file into a table.*****)**

* You need to first create a table with all the columns
* Second, prepare CSV data

#### Example 1

```sql
COPY persons(first_name, last_name, dob, email)
FROM 'C:\sampledb\persons.csv'
DELIMITER ','
CSV HEADER;
```

PostgreSQL gives back the following message:

```sql
COPY 2
```

It means that two rows have been copied.

#### Example 2

If CSV file contains all columns of the table, you don’t need to specify them explicitly

```sql
COPY sample_table_name
FROM 'C:\sampledb\sample_data.csv'
DELIMITER ','
CSV HEADER;
```

***

### Export PostgreSQL Table to CSV file

**(*****show you how to export tables to a CSV file.*****)**

If you want to export all columns of a table

```sql
COPY persons TO 'C:\tmp\persons_db.csv' DELIMITER ',' CSV HEADER;
```

If you want to export specific columns of a table

```sql
COPY persons(first_name,last_name,email)
TO 'C:\tmp\persons_partial_db.csv' DELIMITER ',' CSV HEADER;
```

***


---

# 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://notes.nomanaziz.me/development/database/sql/basics/11.-import-and-export-data.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.
