Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Postgres Database into CSV file

I want to export a Postgres database into a CSV file. Is this possible?

If it is possible, then how can I do this? I have seen that we can convert a particular table into a CSV file but I don't know about a whole database.

like image 304
Anand Singh Avatar asked Jul 04 '13 06:07

Anand Singh


People also ask

Can you export a PostgreSQL database?

You can export a PostgreSQL database to a file by using the pg_dump command line program, or you can use phpPgAdmin.

How do I export data from pgAdmin 4 to CSV?

In PgAdmin export option is available in file menu. Execute the query, and then we can view the data in the Output pane. Click on the menu FILE -> EXPORT from query window.

How do I Download a table from PostgreSQL?

In TablePlus, right-click on the table name in the left sidebar and choose Export... . You can also select the table then navigate to menu File > Export… and choose CSV output.


5 Answers

I made this pl/pgsql function to create one .csv file per table (excluding views, thanks to @tarikki):

CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
declare
   tables RECORD;
   statement TEXT;
begin
FOR tables IN 
   SELECT (table_schema || '.' || table_name) AS schema_table
   FROM information_schema.tables t INNER JOIN information_schema.schemata s 
   ON s.schema_name = t.table_schema 
   WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
   AND t.table_type NOT IN ('VIEW')
   ORDER BY schema_table
LOOP
   statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER';
   EXECUTE statement;
END LOOP;
return;  
end;
$$ LANGUAGE plpgsql;

And I use it this way:

SELECT db_to_csv('/home/user/dir');
-- this will create one csv file per table, in /home/user/dir/
like image 89
jllodra Avatar answered Oct 03 '22 00:10

jllodra


You can use this at psql console:

\copy (SELECT foo,bar FROM whatever) TO '/tmp/file.csv' DELIMITER ',' CSV HEADER

Or it in bash console:

psql -P format=unaligned -P tuples_only -P fieldsep=\, -c "SELECT foo,bar FROM whatever" > output_file
like image 29
Piotr Olaszewski Avatar answered Oct 03 '22 01:10

Piotr Olaszewski


Modified jlldoras brilliant answer by adding one line to prevent the script from trying to copy views:

CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
declare
   tables RECORD;
   statement TEXT;
begin
FOR tables IN 
   SELECT (table_schema || '.' || table_name) AS schema_table
   FROM information_schema.tables t INNER JOIN information_schema.schemata s 
   ON s.schema_name = t.table_schema 
   WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema', 'configuration')
   AND t.table_type NOT IN ('VIEW')
   ORDER BY schema_table
LOOP
   statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER';
   EXECUTE statement;
END LOOP;
return;  
end;
$$ LANGUAGE plpgsql;
like image 37
tarikki Avatar answered Oct 03 '22 02:10

tarikki


If you want to specify the database and user while exporting you can just modify the answer given by Piotr as follows

psql -P format=unaligned -P tuples_only -P fieldsep=\, -c "select * from tableName" > tableName_exp.csv -U <USER> -d <DB_NAME>
like image 20
smishra Avatar answered Oct 03 '22 01:10

smishra


COPY tablename TO '/tmp/products_199.csv' DELIMITER ',' CSV HEADER;

check this out

like image 40
Tombeau Avatar answered Oct 03 '22 00:10

Tombeau