Logging a PostgreSQL session in text file - postgresql

I am trying to log a complete session in psql into a .txt file. The command given to me was initially this:
psql db_name| tee file_name.txt
However, my SSH client does nothing until I quit it. That means, it does not recognize any command. More like a document, no action happens no matter what I write. So far, only '\q' is recognised which lets me get out of it. Any ideas what is happening? How am I to write the query if shell will not read anything. Also, I tried the following (this is before connecting to database) :
script filename.txt
It does show the message : script started, file is filename.txt, but I dont know where this file is stored and how to retrieve it.
Any help with the above will be welcome and really appreciated! Thanks a lot :)

There is option to psql for log query and results:
-L filename
--log-file filename
Write all query output into file filename, in addition to the normal output destination.
Try this:
psql db_name -L file_name.txt

Related

Can I pass a .sql file to psql?

I'm trying to pass a sql file to psql. After reading the docs, tried:
psql_args=(
"password='$INPUT_PASSWORD'"
dbname=analytics
"host='$INPUT_HOST'"
user=analytics
port=32648
file=query.sql
)
psql "${psql_args[*]}"
psql: error: invalid connection option "file"
root#380773cb4e26:/#
If I remove the file=query.sql arg this results in a connection to psql. I just don't know how to pass it a query file?
On the docs, two arguments look like ones of interest here:
-f filename
--file=filename
Read commands from the file filename, rather than standard input
and also:
-c command
--command=command
Specifies that psql is to execute the given command string, command
I tried the file=query.sql one but that failed with the error message above.The command one wants a string whereas I want to pass a .sql file. I tried anyway:
psql_args=(
"password='$INPUT_PASSWORD'"
dbname=analytics
"host='$INPUT_HOST'"
user=analytics
port=32648
command=query.sql
)
psql "${psql_args[*]}"
psql: error: invalid connection option "command"
Is there a way that I can pass query.sql to psql in order to run a query?
You seem to be packaging options up into a connection string. But --file must be given directly as an option to psql, not as part of a connection string.
psql "${psql_args[*]}" --file=query.sql
Since other answer seem to overlook this.
Here is how to store dynamic options into an array, and pass it as arguments to the command:
#!/usr/bin/env bash
psql_args=(
"--dbname=analytics"
"--host=$INPUT_HOST"
"--user=analytics"
"--port=32648"
"--file=query.sql"
)
psql "${psql_args[#]}"

Saving presto query output to a file

I'm pretty new to PostgreSQL, but I have to query some results using psql in an interactive command line session. I am connecting through a cluster and I would like to extract the output of the query into a file for further analysis.
The command I use to connect is psql -h hostname.with.dbs -p 5432 -U my-username and inside I do the query. But it is not clear to me how to pipe that into a file in my user folder in the machine used to connect to Presto.
If I have to add more details, let me know, as I am not an expert and might forgot to add important information. Thank you all!
I found a solution to that. Basically appending \g file_name; at the end of the query. It saves the file in the directory where I launched the command to connect to the database. I didn't try to add full path to the file name, but I assume it would work as well.

Importing a dump.sql into Postgres Database using command prompt

I'm using my Windows PC, and I'm trying to import a "dump.sql" into the database "TEST" created with Postgres, using command prompt. I do it in two steps:
Step 1:
cd C:\Program Files\PostgreSQL\12\bin
Step 2:
psql -U username -d TEST < C:\Users\Username\Desktop\University\Politechnic\III year\INFORMATIC TECHNOLOGIES FOR THE WEB\PDF SL\SL\Materials\TIW_IOL_ServletJSP\db\dump.sql`
Long path, I know. But the result is: "Impossible to find specified file".
What can I do?
Not sure how security is where you are at, but can you attempt to write to a file with a simpler destination? Such that you take out any possibility of spaces and/or length being the issue? Then you will at least be able to remove those variables or narrow down to them depending on the outcome. Note that the max path length is 260 characters
(From comment on question)

Postgres cannot open CSV file for read access: Permission denied

I am trying to read a CSV file located on a postgres 8.4 server filesystem:
COPY ip2location_db1 FROM '/pgsrc/IP2LOCATION-LITE-DB9.CSV' WITH CSV QUOTE AS '"';
I am getting the error:
Cannot open file for read access: Permission denied
The file has owner postgres and I tried putting it on /var/lib/pgsql and also on /pgsources folder, to which I gave ownership to postgres user.
What am I doing wrong?
I have run into this issue before, and rather than jockey around with permissions all the time, I just import from STDIN.
This would accomplish what you want (albeit not precisely the way you want to do it), but I think it's a lot less cumbersome and error-prone. Try:
cat /pgsrc/IP2LOCATION-LITE-DB9.CSV | psql -c "COPY ip2location_db1 FROM STDIN (FORMAT CSV);"
This does imply that you're running the query from a shell script or something, but to implement it the other way, you'd have to incorporate the change of permissions with a shell script or something.
(Also, according to the docs, the default quote is the double quote, so you don't need to specify the quote.)

Syntax of Wget for windows

Does anybody know syntax for wget command in windows. I tried its basic syntax but the problem is file gets downloaded in the directory on which I have opened command prompt. I want to know whether we can explicitly specify destination in its command. If possible then let me know that would be much helpful for me.
If anyone reading this wants to save files downloaded to a directory, use "-P".
Example:
wget LINKHERE -P %USERPROFILE%/Downloads
This saves whatever is served by your link to C:\Users\username\Downloads.
According to the manual -O, --output-document=FILE write documents to FILE.
So you must give a file name after a valid directory as such:
C:\cronjobs>wget -q -O C:\Users\Public\Documents\tmp1.txt "http://google.com/"
note: -q option is to say quiet but -O is to say save file to a given file name and it will work!
Sure you can.
Use the -O syntax, and the path to use.
I've just tested this with:
C:\users\julien>wget google.com -O "C:\here.html"
And "here.html" was google's index page on the root of my "C:" drive