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.
Related
I am creating a powershell script to read data from a postgres DB
But any lines given after the psql.exe command does not works
after the psql.exe line the console asks for the password
and does nothing it's only when I press Ctrl+C the other lines get executed
I tried using Start-Job but then I am unable to read the output of my select command it only returns the following line
Job started: System.Management.Automation.PSRemotingJob stating that the job has started
I also tried the Invoke-Command but that too didn't help.
Can anyone help me with a simple sample that explains how to enter password for the psql.exe cmd and how to read the output from the select cmd
I am sharing the approach that worked for me
$env:PGPASSWORD='password'
$result=Write-Output "Select * FROM public.table_name" | & $psql -h 127.0.0.1 -p 5432 -U -U postgres -d database_name
Now you can access the output of the select from the result variable.
You can use a for method and iterate over result to read each row.
i am trying to execute psql queries from the bash command line passing password in following format
set PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
Somehow my queries are not giving any results.i have tried to pass different queries or incorrect credentials but still script execute without any error.
If i don't pass password and try logging in to command prompt,everything works fine.
i want to check what basic thing i am missing above
Below command worked
PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
remove set at start of command fixed it
I am trying to execute multiple sql scripts using psql. I created one master script with all the scripts to be executed as below. I want all the scripts to succeed or fail together.
master.sql
BEGIN;
\i one.sql
\i two.sql
\i three.sql
COMMIT;
I am trying to catch the error code of psql to determine if it success. When i query for ?$ it always returns 0. I figured when add ON_ERROR_STOP=1 to command it returns proper error code. Problem with this approach if the error happens in three.sql it does not roll back one.sql and two.sql transactions.
psql -U postgres -h localhost -d test -v ON_ERROR_STOP=1 -f master.sql
What would be correct approach to find out if script executed successfully.
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;
When I run data-only script in SQL Server 2008 R2, it is showing this error:
Cannot execute script
Additional information:
Exception of type 'System.OutOfMemoryException' was thrown. (mscorlib)
The size of script file is 115MB and it's only data .
When I open this script file, it shows:
Document contains one or more extremely long lines of text.
These lines cause the editor to respond slowly when you open the file .
Do you still want to open the file ?
I run schema-only script first and then data-only script .
Is there any way to fix this error ?
I solved it by using sqlcmd utitlity.
sqlcmd -S "Server\InstanceName" -U "instantName" -P "password" -i FilePathForScriptFile
For example :
sqlcmd -S .\SQLEXPRESS -U sa -P 123 -i D:\myScript.sql
Zey's answer was helpful for me, but for completion:
If you want to use Windows Authentication just omit the user and password.
And don't forget the quotes before and after the path if you have spaces.
sqlcmd -S .\SQLEXPRESS -i "C:\Users\Stack Overflow\Desktop\script.sql"
If you're logged into the domain with the correct privileges and there's only one instance running, you also do not have to provide the above user/pw/instance command args. I was able to just execute:
sqlcmd -i myfile.sql