使用本地提供的列表从远程 postgresql 数据库中删除记录

Deleting records from a remote postgresql database using locally supplied list

我在一个有效的 bash 脚本中有以下逻辑:

 #copy records to delete file to respective servers.  faster than running locally
 scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $dir/$server.records_to_delete.txt $server:/tmp/

 # trigger the deletion on remote machine.
 deletion_status=$(psql -U test testdb -h $server -c "CREATE TEMP TABLE tmp_cdr (id int); COPY tmp_widgets FROM '/tmp/$server.records_to_delete.txt'; DELETE FROM widgets USING tmp_cdr WHERE widgets.id = tmp_widgets.id;")

但我正在尝试合并为一个命令。因此,我不想通过 scp 将记录列表删除到远程服务器,而是将其保存在本地,并以某种方式使用本地文件创建 tmp_widgets。

如有任何建议,我们将不胜感激

您不需要将文件复制到服务器。您正在寻找的是 STDIN for COPY:

cat records_to_delete.txt | psql $server -c "COPY tmp_widgets FROM STDIN;"