如何将 psql 输出保存到文件?

发布于 2024-10-22 02:00:47 字数 101 浏览 6 评论 0原文

我正在使用 psql 的 \dt 列出数据库中的所有表,我需要保存结果。

psql 命令的结果导出到文件的命令是什么?

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

What is the command to export the results of a psql command to a file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

疯狂的代价 2024-10-29 02:00:47

来自 psql 的帮助(\?):

\o [FILE] 将所有查询结果发送到文件或|管道

命令序列将如下所示:

[wist@scifres ~]$ psql db
Welcome to psql 8.3.6, the PostgreSQL interactive terminal

db=>\o out.txt
db=>\dt

然后任何 db 操作输出都将写入 out.txt。
输入“\o”将输出恢复到控制台。

db=>\o

From psql's help (\?):

\o [FILE] send all query results to file or |pipe

The sequence of commands will look like this:

[wist@scifres ~]$ psql db
Welcome to psql 8.3.6, the PostgreSQL interactive terminal

db=>\o out.txt
db=>\dt

Then any db operation output will be written to out.txt.
Enter '\o' to revert the output back to console.

db=>\o
凉城 2024-10-29 02:00:47

jhwist 已经描述了 psql \o 命令。

另一种方法是使用 COPY TO 命令直接写入服务器上的文件。这样做的优点是它以您选择的易于解析的格式转储,而不是 psql 的表格格式。使用 COPY FROM 导入到另一个表/数据库也非常容易。

注意!这需要超级用户pg_write_server_files权限,并将写入服务器上的文件。

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

创建带有 ';' 的 CSV 文件作为字段分隔符。

与往常一样,有关详细信息,请参阅文档

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 or pg_write_server_files 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

后eg是否自 2024-10-29 02:00:47

使用pgsql命令的o参数。

-o, --output=FILENAME 将查询结果发送到文件(或管道)

psql -d DatabaseName -U UserName -c "SELECT * FROM TABLE" -o /root/Desktop/file.txt

Use o parameter of pgsql command.

-o, --output=FILENAME send query results to file (or |pipe)

psql -d DatabaseName -U UserName -c "SELECT * FROM TABLE" -o /root/Desktop/file.txt
请持续率性 2024-10-29 02:00:47

\copy 这是一个 postgres 命令,适用于任何用户。不知道它是否适用于 \dt,但从以下链接复制了一般语法 Postgres SQL 复制语法

\copy (select * from tempTable limit 100) to 'filenameinquotes' with header delimiter as ','

上面会将选择查询的输出保存在作为 csv 文件提供的文件名中

编辑:

对于我的 psql 服务器,以下命令有效 这是旧版本 v8 .5

copy (select * from table1) to 'full_path_filename' csv header;

\copy which is a postgres command can work for any user. Don't know if it works for \dt or not, but general syntax is reproduced from the following link Postgres SQL copy syntax

\copy (select * from tempTable limit 100) to 'filenameinquotes' with header delimiter as ','

The above will save the output of the select query in the filename provided as a csv file

EDIT:

For my psql server the following command works this is an older version v8.5

copy (select * from table1) to 'full_path_filename' csv header;
白云不回头 2024-10-29 02:00:47

使用以下查询将结果存储在 CSV 文件中

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

示例

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

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' csv header;
多孤肩上扛 2024-10-29 02:00:47

如果出现以下错误,

ufgtoolspg=> COPY (SELECT foo, bar FROM baz) TO '/tmp/query.csv' (format csv, delimiter ';');
ERROR:  must be superuser to COPY to or from a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.

可以按以下方式运行:

psql somepsqllink_or_credentials -c "COPY (SELECT foo, bar FROM baz) TO STDOUT (format csv, delimiter ';')"  > baz.csv

If you got the following error

ufgtoolspg=> COPY (SELECT foo, bar FROM baz) TO '/tmp/query.csv' (format csv, delimiter ';');
ERROR:  must be superuser to COPY to or from a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.

you can run it in this way:

psql somepsqllink_or_credentials -c "COPY (SELECT foo, bar FROM baz) TO STDOUT (format csv, delimiter ';')"  > baz.csv
北城半夏 2024-10-29 02:00:47
COPY tablename TO '/tmp/output.csv' DELIMITER ',' CSV HEADER;

该命令用于将整个表存储为 csv

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

this command is used to store the entire table as csv

眸中客 2024-10-29 02:00:47

我假设存在一些内部 psql 命令,但您也可以从 util-linux-ng 包:

描述
脚本将终端上打印的所有内容制作为打字稿。

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.

檐上三寸雪 2024-10-29 02:00:47

的 docker 方法

通过 psql 命令

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

或从 sql 文件查询

docker exec -i %containerid% psql -U %user% < file.sql > data.txt

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
梦旅人picnic 2024-10-29 02:00:47

这种方法适用于从最简单到最复杂的任何 psql 命令,无需对原始命令进行任何更改或调整。

注意:适用于 Linux 服务器。


  • 将命令的内容保存到文件中

模型

read -r -d '' FILE_CONTENT << 'HEREDOC'
[COMMAND_CONTENT]

HEREDOC
echo -n "$FILE_CONTENT" > sqlcmd

示例

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
  • 运行命令 模型

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

示例

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

  • 查看/跟踪命令输出

cat sqlop

完成!谢谢! =D

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文