I have a sql script which i run for by-hand status checks of a long running process. My server is currently postgres 10. The script contains a couple dozen queries. However, each query requires the id of the current process (not linux pid, but a column in the db).
So, it's like
SELECT * FROM table1 WHERE id = 'xxx';
SELECT * FROM table2 WHERE id = 'xxx';
... and so forth
I have a sql script that i keep updating with the most recent id, and run with
db# \i my-script.sql
It would be great if I could pass variables into the script. For example, maybe the script is
SELECT * FROM table1 WHERE id = '$1';
SELECT * FROM table2 WHERE id = '$1';
and i could run
db# \i my-script.sql 'xxx'
and it would properly substitute that value for all $1 in the sql script. Bonus points for multiple args (eg $2 and so on).
OTOH, I could write a bash script wrapping psql command line, but I'd need to drop to a shell to execute it - if i'm already in psql, I'm right there.
Any advice?
Thanks!
Use psql variables.
In the script, write
\prompt 'Please enter id:' myid
SELECT * FROM table1 WHERE id = :'myid';
If you don't want an interactive prompt, omit the \prompt and set the variable in the calling script:
\set myid 1234
I am migrating oracle code to postgresql where i need to append the query to existing log file.
Basically i want equivalent of oracle command " SPOOL test.log APPEND " in PostgreSQL . Is there a way to do that?
I tried to append new data to the log file using \o or \o+ or copy in PostgreSQL but it overwrites the log file.
My code is something like this :
Oracle:
spool test.log
select uid from users where uid='1111';
spool off
select sysdate from dual;
//other business logic code
-
spool test.log append
select balance from balances where uid='1111';
spool off
Postgresql:
\o test.log
select uid from users where uid='1111';
\o
select current_date;
//other business logic code
-
\o test.log
select balance from balances where uid='1111';
\o
I want the two queries in \o block to append to same file in PostgreSQL.
You could use
\o | cat >> test.log
on UNIX platforms.
How I output select into a file?
I try
SELECT * FROM table \g filename
but I get “filename: Permission denied”
Either the file already exists and you have no write permission, or you have no write permission on your current working directory.
Try an absolute path:
SELECT * FROM atable \g /writable/directory/filename
I am using pgAdmin version 4.3 and i want to export one table data to CSV file. I used this query
COPY (select * from product_template) TO 'D:\Product_template_Output.csv' DELIMITER ',' CSV HEADER;
but it shows error
a relative path is not allowed to use COPY to a file
How can I resolve this problem any help please ?
From the query editor, once you have executed your query, you just have to click on the "Download as CSV (F8)" button or use F8 key.
Source pgAdmin 4 Query Toolbar
Use absolute paths or cd to a known location so that you can ignore the path.
For example cd into documents directory then run the commands there.
If you are able to cd into your documents directory, then the command would be like this:
Assuming you are want to use PSQL from the command line.
cd ~/Documents && psql -h host -d dbname -U user
\COPY (select * from product_template) TO 'Product_template_Output.csv' DELIMITER ',' CSV HEADER;
The result would be Product_template_Output.csv in your current working directory(Documents folder).
Again using psql.
You have to remove the double quotes:
COPY (select * from product_template) TO 'D:\Product_template_Output.csv'
DELIMITER ',' CSV HEADER;
If your PgAdmin instance resides in a remote server, the aforementioned solutions might not be handy for you if you do not have remote access to the server. In this case, simply select all the query data and copy it. Open an excel file and you could paste it. Simple !! Tweaked.
You might have tough time if your query result is too much though.
Try this command:
COPY (select * from product_template) TO 'D:\Product_template_Output.csv' WITH CSV;
In PgAdmin export option is available in file menu.Execute the query, and then we can view the data in the Output pane. Click on the menu FILE -> EXPORT from query window.
PSQL to export data
COPY noviceusers(code, name) FROM 'C:\noviceusers.csv' DELIMITER ',' CSV HEADER;
https://www.novicetechie.com/2019/12/export-postgresql-data-in-to-excel-file.html for reference.
Write your query to select data on the query tool and execute
Click on the download button on the pgAdmin top bar (selected in red)
Rename the file to your liking
Select which folder to save the file
Congrats!!!
I have a SQL database. If I export it as CSV using sqlitebrowser there is an option to define a "Quote character". How can I set the quote character when doing the export with a script? The default is " and want it to be #.
Export script without quote-character definition:
sqlite3 my.db<<EOF
.mode csv
.separator |
.output out.csv
SELECT * FROM myTable
EOF
Desired output:
#This is#|# a
"table" with#|# three columns, #
# some
newlines,
and...
#|#two#|# "rows". #
I've looked in the documentation und couldn't find such an option. Does it exist?