Update PostgreSQL table using ssh remote command with shell script - postgresql

Could you help me, I'm trying to perform an update on a table on PostgreSQL by shell script by sending the remote command externally to the server following my code.
Today I am executing a command to select and it is working, but for update I have a return of failure indicating that the column where I will set the information does not exist.
Executing the form I am displaying, I can extract the result from the execution of the query and display it in the terminal, but to carry out the update I have the following return:
result=$(ssh 10.11.12.193 '"(`psql -d totalipdb -U totalip -A -t --command="select host,name,phone_number,active from trunks where id = '$LINHA';"`"');
Result: all information returned for select OK.
bash: (10.11.12.163|URA Desenvolvimento|sip_ura|f: command not found.
I need to run updates too, can you help me?
result=$(ssh 10.11.12.193 '"(`psql -d totalipdb -U totalip -A -t --command="UPDATE trunks SET active = 'f' WHERE id = '55';"`"');
ERROR: column "f" does not exist
LINHA 1: UPDATE trunks SET active = f WHERE id = 55;

Related

Update a PostgreSQL field from the content of a file

I have a file containing a value which should go into a field of a PostgreSQL table.
By searching a little, I found many answers, e.g. How can I update column values with the content of a file without interpreting it? or https://stackoverflow.com/a/14123513/6630397, with this kind of snippet, but it has to be run in a psql terminal:
\set content `cat /home/username/file.txt`
UPDATE table SET field = :'content' WHERE id=1;
It works, but is it possible to programmatically execute it in one shot, directly from a bash prompt, without manually entering the psql command line, e.g. something like:
$ psql -d postgres://postgres#localhost/mydatabase -c \
"UPDATE table SET field = :'the_file_content' WHERE id=1;"
?
There is also the -v argument that seems promising but I'm not successful when using it:
$ psql -d postgres://postgres#localhost/mydatabase \
-v content=`cat ${HOME}/file.txt` \
-c "UPDATE table SET field = :'content' WHERE id=1;"
I've got thousands of psql: warning: extra command-line argument where psql actually seems to "execute" each comma separated strings of the file as pg commands, where it shouldn't of course; the file content, which consists of a single line, must be treated as a whole.
Doc PostgreSQL 14:
https://www.postgresql.org/docs/current/app-psql.html
How about reading the file content into a variable first and then use it?
content=$(<integer_infile); psql -p 5434 -c "update table set field = $content where id = 1;"
content=$(<text_infile); psql -p 5434 -c "update table set field = '$content' where id = 1;"
This at least works for me if the file contains an integer or text including spaces on a single line.

PostgreSQL copy command gives error for temporary table

I am trying to run the below command on the unix console :
env 'PGOPTIONS=-c search_path=admin -c client_min_messages=error' psql -h hostname -U user -p 1111 -d platform -c "CREATE TEMP TABLE admin.tmp_213 AS SELECT * FROM admin.file_status limit 0;\copy admin.tmp_213(feed_id,run_id,extracted_date,file_name,start_time,file_size,status,crypt_flag) FROM '/opt/temp/213/list.up' delimiter ',' csv;UPDATE admin.file_status SET file_size = admin.tmp_213.file_size FROM admin.tmp_213 A WHERE admin.file_status.feed_id = A.feed_id and admin.file_status.file_name = A.file_name;"
I am getting the below error:
ERROR: syntax error at or near "\"
LINE 1: ...* FROM admin.file_status limit 0;\copy admi...
If I use the above command without a backslash before COPY, it gives me the below error:
ERROR: cannot create temporary relation in non-temporary schema
I am doing the above to implement the solution as mentioned here:
How to update selected rows with values from a CSV file in Postgres?
Your first error is because metacommands like \copy cannot be combined in the same line as regular commands when given with -c. You can give two -c options, with one command in each instead.
The second error is self-explanatory. You don't get to decide what schema your temp table goes to. Just omit the schema.

how to stop and exit from the postgresql restoration if an error occurs

we are trying to restore PostgreSQL schema dumps into a database from the command line using the below command
psql -U postgres -d dbname < filename
and we are getting response in command line during the restoration like below
SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
.
.
.
.
and going on
My question is, if there are some errors happened in between the restoration and restoration continues to go on, how to detect that error without scrolling through these entire response through command line? or
Is there anyway to automatically stop and exit from the restoration process if an error occurs?
We are using PostgreSQL 10
There are two things you can do:
First run the entire script as a single transaction:
psql --single-transaction -U postgres -d dbname < filename
Additionally you can configure psql to abort if an error occurs:
\set ON_ERROR_STOP on
You would need to put the \set into the file you are running.
Alternatively, if you always want that behavior, put it into ~/.psqlrc

How to remove all outputs ( PostgreSQL / Windows )

I am using "-f" option to run a external SQL file(COPY command) as following, and want to remove the all outputs including strings which have *** as well.
Is there any way to do that?
psql.exe -f temp.sql -p 5433 -U username -s postgres
***(Single step mode: verify command)*******************************************
COPY TABLENAME FROM STDIN (DELIMITER('#'));
***(press return to proceed or enter x and return to cancel)********************
COPY 13
Any other PostgreSQL parameter to change the mode from "Single step mode" to other one?
Thanks in advance.

Check if one request fails with the command-line

I have those two requests in one file :
select id from "user" where id = 1; // request passes
select from "user"; // request fails
When I run the following commands in my shell :
psql -U username -d db_name -h host -p port -f test.sql
echo $?
This will always prompt me 0 (assuming that the credentials are correct, and the psql command ran without execution error). This seems pretty fair.
I was wondering if there was any way to get a different return code if one of the queries inside the linked file fails ?
Thanks for the help.
On the first line of the script, add \set ON_ERROR_STOP on . psql will return exit code 3 when the script failed.