Create csv using SQL and scp to local machine from Docker container in Kubernetes
(using postgres)
sh into your docker container using
docker exec -ti <docker_container_name> sh
*note: find your docker container name using docker ps
Install postgresql-client using either
apk add postgresql-client
or
apt-get install -y postgresql-client
Step 2: Get into postgres shell
python manage.py dbshell
Step 3: Run the csv command
\copy (SELECT * FROM api_table_name) to 'api_table_name.csv' with csv header;
Inside the parenthesis, you can do any SQL statement that you want to output into a csv. Inside the quotes is whatever you want the csv file to be named and I think you can add the file path you want to use there as well. This command saves it at the top level of your Docker container.
Step 4: Move the file from your Docker container to your server so you can scp it to your local machine
kubectl cp <docker_container_name>:api_table_name.csv api_table_name.csv
The first api_table_name is the name of the csv on the server, the second one is what you want to name it locally when you move it to your computer. Here it is the same name.
Step 5: scp (move) csv to your local machine from server
scp -P <port_id> <your_user_name>@<server_name>:<path_to_api_table_name.csv> ~/ api_table_name.csv
This command moves the file to your root dir ~/ and saves it as api_table_name.csv
This scp command also includes the -P flag if your server has a port number.
Hope this helps someone!