---
title: Copy Data from Postgres to CSV File Using Docker
description: If you need to export data as a .csv file from Postgres and use Docker, here is a simple command to do just that.
date: 2021-03-11
tags: Docker, Postgres
source: https://brianchildress.co/blog/copy-data-docker-to-file
---

# Copy Data from Postgres to CSV File Using Docker

If you need to export data as a _.csv_ file from Postgres and use Docker, here is a simple command to do just that.

Using the [Docker `exec`](https://docs.docker.com/engine/reference/commandline/exec/) command we can COPY data from a PostgreSQL container to a _.csv_ file on the host machine.

```sh
docker exec -u <POSTGRES USER> <CONTAINER NAME> -d <POSTGRES DB> -c "COPY <TABLE> TO STDOUT WITH CSV HEADER" > <file.csv>
```

_Example Use:_

```sh
docker exec -u postgres db-container -d demo -c "COPY users TO STDOUT WITH CSV HEADER" > <users.csv>
```
