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.
Related
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'm dealing with psql for the first time and I need a command that executes a .sql file and after executing, it should make a .log file with the script output. I'm looking for something similar to this other command that I use with SQL Server:
sqlcmd -U userid -P password -S serveraddress -i path_to_the_sql_file -o path_where_to_save_log_file
Can you help me, please? :-)
I would spend some time at the psql page.
A quick example:
psql -U userid -h serveraddress -d some_db -f path_to_the_sql_file -L=path_where_to_save_log_file
With Postgres you need to connect to a database with a client. There are other options for inputting commands and capturing output at the link above.
"CMD.EXE /C " psql -h ipaddress -d dbname -u user -p password -c "\copy table to 'd:/bcptest/file.csv' with delimiter as '|'"
I have to execute this command in pentaho shell. But it is showing as parse error in script. By this command i have to copy data from a postgres table which is remote and save as .csv file in local.Please help.
You do not have to write any code inside pentaho for transferring
data.
You just have to create sample transformation using spoon, which will
have table input step create your database connection with the remote
server.
Then use text file output and store your result.
Understand the concept of ETL Tool before implementing anything.
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.
I am trying to restore the postgres sql data from a file . I am trying to do so but it is not importing .
Here is the command which i am using:
postgres-# psql -hlocalhost -p5432 -u postgres -d test -f C:/wamp/www/test/database_backups/backup004.sql
Please help me what I am doing wrong .
I am using windows and the above command does not throws any error but it does not import data.
Regards
Surjan
The only immediate thing I can see there is the capitilsation of -u for username (should be -U).
Correction: You're typing the command line into the psql shell.
You should exit to the CMD.EXE shell, and try the command there. With the correct capitalisation of -U, by the way.
OR, use this to replay the script into that psql shell:
\i C:/wamp/www/test/database_backups/backup004.sql
The forward slashes don't cause a problem on my Windows machine.