Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

bash - Deleting records from a remote postgresql database using locally supplied list

I have the following logic in a bash script that works:

 #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;")

But I'm trying to consolidate into one command. So instead of scp'ing the list of records to delete over to the remote server, I'd like to keep it local and just somehow create tmp_widgets using the local file.

Any suggestions would be appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to copy the file to the server. What you're looking for is the STDIN for COPY:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...