I am trying to export data from a select statement to a csv where the filename is made up in part by the date-time stamp.
eg export_2014-12-23-14-56 (export_yyyy_mm_dd_hh_mm) but I would take any format.
The code that currently generates the export is
cd c:\program files\postgresql\9.2\bin
psql -U postgres -h 10.0.0.69 -d mbs01 -f -t -A -F"," -c "select * from bespoke.fedex_export" > y:\warehouse\fedex\fedex_export2.txt -L e:\fedex_export.txt
This is really more of a windows question than a postgresql question as you're dealing with how to build a file name on the command line. You can use the %time% and %date%.
If you just call %date% for example, it will output a full string like:
>echo %date%
Tue 12/23/2014
So, you'll want to take substrings from that date to build your filename. The first being the year:
>echo %date:~-4,4%
2014
This starts at index -4 (4 characters from the end of the string) and gives you 4 characters of output.
You can also use positive indexes like so:
echo %date:~10,4%
2014
To generate your desired format, select just the substrings from %date% and %time% that you want like so:
psql -U postgres -h 10.0.0.69 -d mbs01 -f -t -A -F"," -c "select * from bespoke.fedex_export" > export_%date:~-4,4%_%date:~-7,2%_%date:~-10,2%_%time:~0,2%_%time:~3,2%.txt
Related
I try to perform follow command
psql -c '\\COPY (SELECT file_name, status, reported, operator_id, load_dt AT TIME ZONE GMT FROM mytable) TO STDOUT' > myfile
I receive an error
ERROR: column "gmt" does not exist
LINE 1: ...atus, reported, operator_id, load_dt AT TIME ZONE GMT FROM p...
Can this be of help for you? This is on windows machine. Consider that query command might need modification, depending of what your PC operating system is.
psql -U postgres -d aambackend_dev -c "SELECT \"createdAt\"::timestamp without time zone FROM \"User\" limit(2)" -H -o "D:\xxx1.html"
-U postgres - user
-d typeorm - my database to which i want to connect
-c ... - my query command
-H format html, -o output file
For more info you can refer to: https://www.postgresql.org/docs/13/app-psql.html
Note 1: depending of column i want to select, for majority of columns i don't need escape backslash character. Only for columns of type timestamp with/out time zone i needed to add backslash for quotes.
Note 2: semicolon at the end was not needed in this case (in some other commands and cases it is necessary)
This is my exported file:
If you wish CSV format:
psql -U postgres -d aambackend_dev -c "SELECT "email",\"createdAt\"::timestamp without time zone FROM \"User\" limit(2)" -o "D:\xx2212.csv"
When I using this command to backup postgres database,the backup data echo to screen:
screen /usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
How to avoid it?When using this command :
/usr/pgsql-9.6/bin/pg_dump -v -h prod-book-db -U postgres dolphin > ./dolphin-fulldb-backup-201904130913.bak
This only echo backup log,do not contains content.
this is probably a bit late but there's an 'f' flag for this:
-f file
--file=file
Send output to the specified file. This parameter can be omitted for file based output formats, in which case the standard output is used. It must be given for the directory output format however, where it specifies the target directory instead of a file. In this case the directory is created by pg_dump and must not exist before.
So you can use it like this:
screen /usr/pgsql-9.6/bin/pg_dump -v -f dolphin-fulldb-backup-201904130913.bak -h prod-book-db -U postgres dolphin
Suppose I created a sequence in postgresql:
CREATE SEQUENCE my_seq;
I store the below line in an sql file get_seq.sql
SELECT last_value FROM my_seq;
$SUDO psql -q -d database_bame -f get_seq.sql
How do I get the int number returned by SELECT into bash and use it?
You can capture the result of a command using the VAR=$(command) syntax:
VALUE=$(psql -qtAX -d database_name -f get_seq.sql)
echo $VALUE
The required psql options mean:
-t only tuple
-A output not unaligned
-q quiet
-X Don't run .psqlrc file
Try:
LAST_VALUE=`echo "SELECT last_value FROM my_seq;" | psql -qAt -d database_name`
In postgresql 9.3.1, when interactively developing a query using the psql command, the end result is sometimes to write the query results to a file:
boron.production=> \o /tmp/output
boron.production=> select 1;
boron.production=> \o
boron.production=> \q
$ cat /tmp/output
?column?
----------
1
(1 row)
This works fine. But how can I get the query itself to be written to the file along with the query results?
I've tried giving psql the --echo-queries switch:
-e, --echo-queries
Copy all SQL commands sent to the server to standard output as well.
This is equivalent to setting the variable ECHO to queries.
But this always echoes to stdout, not to the file I gave with the \o command.
I've tried the --echo-all switch as well, but it does not appear to echo interactive input.
Using command editing, I can repeat the query with \qecho in front of it. That works, but is tedious.
Is there any way to direct an interactive psql session to write both the query and the query output to a file?
You can try redirecting the stdout to a file directly from your shell (Win or Linux should work)
psql -U postgres -c "select 1 as result" -e nomedb >> hello.txt
This has the drawback of not letting you see the output interactively. If that's a problem, you can either tail the output file in a separate terminal, or, if in *nix, use the tee utility:
psql -U postgres -c "select 1 as result" -e nomedb | tee hello.txt
Hope this helps!
Luca
I know this is an old question, but at least in 9.3 and current versions this is possible using Query Buffer meta-commands shown in the documentation or \? from the psql console: https://www.postgresql.org/docs/9.3/static/app-psql.html
\w or \write filename
\w or \write |command
Outputs the current query buffer to the file filename or pipes it to the shell command command.
Please try this format as I got the output from the same:
psql -h $host -p $port -q -U $user -d $Dbname -c "SELECT \"Employee-id\",\"Employee-name\" FROM Employee_table" >> Employee_Date.csv
I need the output in a CSV file.
I want to store following postgreSQL query result in a variable. I am writing command on the shell script.
psql -p $port -c "select pg_relation_size ('tableName')" postgres
I need variable to save the result on a file. I have tried following but it is not working
var= 'psql -p $port -c "select pg_relation_size ('tableName')" '
Use a shell HERE document like:
#!/bin/sh
COUNT=`psql -A -t -q -U username mydb << THE_END
SELECT count (DISTINCT topic_id) AS the_count
FROM react
THE_END`
echo COUNT=${COUNT}
The whole psql <<the_end ... stuff here ... the_end statement is packed into backticks
the output of the execution of the statement inside the backticks is used as a value for the COUNT shell variable
The -A -t -q are needed to suppress column headers and error output
inside a here document, shell variable substitution works, even in single quotes!
So, you could even do:
#!/bin/sh
DB_NAME="my_db"
USR_NAME="my_name"
TBL_NAME="my_table"
COL_NAME="my_column"
COUNT=`psql -A -t -q -U ${USR_NAME} ${DB_NAME} << THE_END
SELECT COUNT(DISTINCT ${COL_NAME} ) AS the_count
FROM ${TBL_NAME}
THE_END`
echo COUNT=${COUNT}
to run a query inline you have to wrap it in grave accents, not single quotes:
$ vim `which fancyexecfileinpath`
psql lets you run queries from command line, but I guess you should be inputting complete information. you might be missing the database name.
postgres#slovenia:~$ psql -d mydbname -c "select * from applications_application;"
postgres#slovenia:~$ specialvar=`psql -d flango -c "select * from applications_application;"`
postgres#slovenia:~$ echo $specialvar
id | name | entities | folder | def_lang_id | ... | 2013-07-09 15:16:57.33656+02 | /img/app3.png (1 row)
postgres#slovenia:~$
notice the grave accents when assigning it to specialvar
otherwise you'll be setting it to a string.
There shouldn't be any space between the variable and the equals sign ("=") and the value ( http://genepath.med.harvard.edu/mw/Bash:HOW_TO:_Set_an_environment_variable_in_the_bash_shell )