首页 > 解决方案 > 将“tail -f”输出重定向到 COPY 命令

问题描述

我正在尝试将 tail -f -n 1 的输出重定向到 Postgres COPY 命令,要求是对 tail 命令的每个输出执行 COPY 命令。

出来了以下内容:

    tail -f -n 1 <source_file> | xargs -n 1 psql -c 'copy <table_name> from stdin'

但这不起作用,因为 tail 命令的输出被用作 psql 命令的参数而不是标准输入。

还有更通用的:

    tail -f -n 1 <source_file> | psql -tc "copy <table_name> from stdin" 

不起作用,因为 COPY 命令在流的末尾而不是对每一行执行提交。

标签: bashpostgresql

解决方案


意识到问题在于 COPY 不打算接受流,并且数据仅在 COPY 命令结束时才可用(程序中的数据结束),在这种特定情况下,程序从未终止,因为它打算作为消费者工作。


推荐阅读