PSQL multiple queries output to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PSQL multiple queries output to file
# 1  
Old 03-20-2017
Hammer & Screwdriver PSQL multiple queries output to file

Hi,

I have a shell script containing multiple PSQL queries for which I want the output to be redirected to a text file.

Code:
psql -U postgres -d database -o textfile.txt << EOF
Query1;
Query2;
Query ....;
EOF

When executing the script, queries outputs are directed to textfile.txt, however when script was set in cron, textfile.txt is not being updated.
Does anyone have a different way how I can output query results to a text file.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-20-2017 at 08:15 AM.. Reason: Added CODE tags.
# 2  
Old 03-20-2017
You probably need to specify a complete file:
-o /path/to/textfile.txt

cron runs as root, then su to the owner of the crontab file. It DOES NOT execute any of the login scripts. So there is a chance that the script is running in some other current directory.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-20-2017
Thanks @jim_mcnamara, that works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Shell Programming and Scripting

Psql output into array and read 2 fields into different variables

Hello Just edited the entry to make it easier to understand what i want How can i achieve this: GOAL: read 2 field from a table with PSQL result of this PSQL command is this INSTALLEDLANG=$(su - postgres -c "psql -A -t -q -c -d ${DBNAME} -t -c 'SELECT code, iso_code from res_lang'") ... (0 Replies)
Discussion started by: winston6071
0 Replies

3. Shell Programming and Scripting

Help with storing the output of multiple sql queries to a file

Hi All, I have a file queries.txt as follows : SELECT COLUMN1 FROM SCHEMA2.TABLE1 MINUS SELECT COLUMN1 FROM SCDEMA2.TABLE2; SELECT COLUMN2 FROM SCHEMA2.TABLE1 MINUS SELECT COLUMN2 FROM SCDEMA2.TABLE2; SELECT COLUMN3 FROM SCHEMA2.TABLE1 MINUS SELECT COLUMN3 FROM SCDEMA2.TABLE2; SELECT... (2 Replies)
Discussion started by: SriRamKrish
2 Replies

4. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

5. UNIX for Dummies Questions & Answers

multiple queries in single sqlplus

I need to connect a databas eusing sqlplus and i need to store the results of each query in separate file. is it possible to achieve this usng single sqlplus? i dont want to connect each time for each query. thanks (3 Replies)
Discussion started by: pandeesh
3 Replies

6. UNIX for Advanced & Expert Users

Connecting once using sqlplus and doing multiple queries

Hello everyone, It's my first week using unix and shell scripting. I tried creating a script that has a function that execute SQL query. my script looks something like this: ---------------------------------------------------- #!/bin/sh tableName="myTable" secondTable="secondTable"... (2 Replies)
Discussion started by: edlin_r
2 Replies

7. Shell Programming and Scripting

psql output without return \n

Hi Everyone, When i finish running # echo `psql -t -U root databaseA -c "select a, b from book"`; i get the output 107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101 Would like to have the result like: 107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101 (7 Replies)
Discussion started by: jimmy_y
7 Replies

8. Shell Programming and Scripting

grep -f, how to output failed queries

Hi folks, I've been using grep -f file1 file2 to get lines that match a file1 line from file2. I'm now interested in getting the lines from file1 where there was no match in file2. I believe the -v option will give me unmatched lines from file2, but I'm looking for unmatched lines from... (3 Replies)
Discussion started by: sanjuro
3 Replies

9. Shell Programming and Scripting

Multiple MySql queries in shell script?

Hi guys, i know how to run a single query using mysql embedded in a shell script as follows: `mysql -umyuser -pmypass --host myhost database<<SQL ${query}; quit SQL` However, how would i be able to run several queries within the same connection? The reason for this is i am creating... (3 Replies)
Discussion started by: muay_tb
3 Replies

10. Shell Programming and Scripting

Run SQL queries in DB2 and output to file

Hi, I new to Unix and scripting. Following is my requirement. Can someone tell me whether its possible or not. Also please let me know how to proceed further if this is possible. List of queries are stored in a file. For example, I have to run a query like this: Select * from &XYZ where... (0 Replies)
Discussion started by: simhasuri
0 Replies
Login or Register to Ask a Question