<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Exporting CSV data from the server
This process is slightly cumbersome because of Unix permissions.  Remember - nine times out of ten, on Unix, it's probably a permissions problem.
In this case the user 'postgres' which runs the PostgreSQL server doesn't have write permissions to your home directory /home/jovyan/work.  To work around it, we write to a shared space, /tmp, from PostgreSQL, then copy the file to your own directory.
Standard db setup
Step1:  A little database example
Step2:  Exporting to CSV
Now that you see we have a real table with real data, we export using the same COPY command we use for import.  The main differences are
Step3:  We can see that the file correctly exported.
Step4:  Now move the file to a space you can reach from Jupyter
<ASSISTANT_TASK:>
Python Code:
!echo 'redspot' | sudo -S service postgresql restart
%load_ext sql
!createdb -U dbuser test
%sql postgresql://dbuser@localhost:5432/test
Explanation: Exporting CSV data from the server
This process is slightly cumbersome because of Unix permissions.  Remember - nine times out of ten, on Unix, it's probably a permissions problem.
In this case the user 'postgres' which runs the PostgreSQL server doesn't have write permissions to your home directory /home/jovyan/work.  To work around it, we write to a shared space, /tmp, from PostgreSQL, then copy the file to your own directory.
Standard db setup
End of explanation
%%sql
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
    id SERIAL,
    s TEXT
);
%%sql
INSERT INTO foo (s) VALUES 
('hi'),
('bye'),
('yo')
;
%%sql
SELECT * FROM foo;
Explanation: A little database example
End of explanation
%%sql
COPY
(SELECT * FROM foo ORDER BY s)
TO '/tmp/testout.csv'
WITH
CSV
HEADER 
DELIMITER ','
QUOTE '"';
Explanation: Exporting to CSV
Now that you see we have a real table with real data, we export using the same COPY command we use for import.  The main differences are:
COPY ... TO instead of COPY ... FROM
You may specify an arbitrarily complex query, using multiple tables, etc.
Note the /tmp/ location of the output file; this is our shared space.
Read all the details about pgsql's non-standard-SQL COPY function at https://www.postgresql.org/docs/9.5/static/sql-copy.html.
End of explanation
!cat /tmp/testout.csv
!csvlook /tmp/testout.csv
Explanation: We can see that the file correctly exported.
End of explanation
!cp /tmp/testout.csv /home/jovyan/work/testout.csv
Explanation: Now move the file to a space you can reach from Jupyter:
End of explanation
<END_TASK>