psql - save results of command to a file

后端 未结 10 1178
日久生厌
日久生厌 2020-12-12 09:10

I\'m using psql\'s \\dt to list all tables in a database and I need to save the results.

What is the syntax to export the results of a psql command to a

相关标签:
10条回答
  • 2020-12-12 09:35

    I assume that there exist some internal psql command for this, but you could also run the script command from util-linux-ng package:

    DESCRIPTION Script makes a typescript of everything printed on your terminal.

    0 讨论(0)
  • 2020-12-12 09:37

    Approach for docker

    via psql command

     docker exec -i %containerid% psql -U %user% -c '\dt' > tables.txt
    

    or query from sql file

    docker exec -i %containerid% psql -U %user% < file.sql > data.txt
    
    0 讨论(0)
  • 2020-12-12 09:45

    The psql \o command was already described by jhwist.

    An alternative approach is using the COPY TO command to write directly to a file on the server. This has the advantage that it's dumped in an easy-to-parse format of your choice -- rather than psql's tabulated format. It's also very easy to import to another table/database using COPY FROM.

    NB! This requires superuser privileges and will write to a file on the server.

    Example: COPY (SELECT foo, bar FROM baz) TO '/tmp/query.csv' (format csv, delimiter ';')

    Creates a CSV file with ';' as the field separator.

    As always, see the documentation for details

    0 讨论(0)
  • 2020-12-12 09:46

    Use the below query to store the result in a CSV file

    \copy (your query) to 'file path' csv header;
    

    Example

    \copy (select name,date_order from purchase_order) to '/home/ankit/Desktop/result.csv' cvs header;
    

    Hope this helps you.

    0 讨论(0)
  • 2020-12-12 09:46
    COPY tablename TO '/tmp/output.csv' DELIMITER ',' CSV HEADER;
    

    this command is used to store the entire table as csv

    0 讨论(0)
  • 2020-12-12 09:46

    This approach will work with any psql command from the simplest to the most complex without requiring any changes or adjustments to the original command.

    NOTE: For Linux servers.


    • Save the contents of your command to a file

    MODEL

    read -r -d '' FILE_CONTENT << 'HEREDOC'
    [COMMAND_CONTENT]
    
    HEREDOC
    echo -n "$FILE_CONTENT" > sqlcmd
    

    EXAMPLE

    read -r -d '' FILE_CONTENT << 'HEREDOC'
    DO $f$
    declare
        curid INT := 0;
        vdata BYTEA;
        badid VARCHAR;
        loc VARCHAR;
    begin
    FOR badid IN SELECT some_field FROM public.some_base LOOP
        begin
        select 'ctid - '||ctid||'pagenumber - '||(ctid::text::point) [0]::bigint
            into loc
            from public.some_base where some_field = badid;
            SELECT file||' '
            INTO vdata
            FROM public.some_base where some_field = badid;
        exception
            when others then
            raise notice 'Block/PageNumber - % ',loc;
                raise notice 'Corrupted id - % ', badid;
                --return;
        end;
    end loop;
    end;
    $f$;
    
    HEREDOC
    echo -n "$FILE_CONTENT" > sqlcmd
    
    • Run the command

    MODEL

    sudo -u postgres psql [some_db] -c "$(cat sqlcmd)" >>sqlop 2>&1

    EXAMPLE

    sudo -u postgres psql some_db -c "$(cat sqlcmd)" >>sqlop 2>&1

    • View/track your command output

    cat sqlop

    Done! Thanks! =D

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