Export Postgres Database into CSV file

后端 未结 7 1950
醉梦人生
醉梦人生 2021-01-29 23:11

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

相关标签:
7条回答
  • 2021-01-29 23:28

    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
    
    0 讨论(0)
  • 2021-01-29 23:32

    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/
    
    0 讨论(0)
  • 2021-01-29 23:32

    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;
    
    0 讨论(0)
  • 2021-01-29 23:39

    Do you want one big CSV file with data from all tables?

    Probably not. You want separate files for each table or one big file with more information that can be expressed in CSV file header.

    Separate files

    Other answers shows how to create separate files for each table. You can query database to show you all tables with such query:

    SELECT DISTINCT table_name
    FROM information_schema.columns
    WHERE table_schema='public'
    AND position('_' in table_name) <> 1
    ORDER BY 1
    

    One big file

    One big file with all tables in CSV format used by PostgreSQL COPY command can be created with pg_dump command. Output will also have all CREATE TABLE, CREATE FUNCTION etc, but with Python, Perl or similar language you can easily extract only CSV data.

    0 讨论(0)
  • 2021-01-29 23:43
    COPY tablename TO '/tmp/products_199.csv' DELIMITER ',' CSV HEADER;
    

    check this out

    0 讨论(0)
  • 2021-01-29 23:45

    I downloaded a copy of RazorSQL, opened the database server and right-clicked on the database and selected Export Tables and it gave me the option of CSV, EXCEL, SQL etc...

    0 讨论(0)
提交回复
热议问题