Extract data from postgres in docker and save it to csv

The case is simple postgres database is hosted in a docker image and to access the database use this command. docker image name can be found with this command docker ps -a.

docker exec -it <docker-image> psql -U postgres

docker exec -it <docker-image> psql -U postgres -a <dbname> -c "copy(select u.name user_name,p.name place_name, attempts,p.created from
place p inner join users u on u.id=p.user_id order by attempts desc) to stdout with csv header" > data-list.csv

Breaking down the command u.name user_name is the field in user table with alias of user_name. p.name place_name is the field in place table with alias of place_name, r.created is the field in place table. place p is naming place table with ‘p’ and inner join joins the user id in both the tables.