I've seen a few related questions here and tried pretty much all the solutions but there's obviously something silly I'm still missing. I'm trying to bypass the need to log in to PostgreSQL using a script. Its a pretty basic script to copy a text file on the server into a table in my database, and works fine except it prompts me to log in to PostgreSQL every time I run it. I intend to write a cron job that performs this task daily so the process needs to be automatic. Here's the script.
export PGPORT=5432
export PGDATABASE=db_name
export PGUSER=user
export PGPASSWORD=my_password
psql -h host_name -p 5432 -U user -W db_name -c "\COPY schema.table_name (col1, col2, col3, col4)
from path_to_txt_file with DELIMITER '^'"
I also went down the ".pgpass file" route to no avail. I saved it in /home/usr/.pgpass, and gave it the following credentials
*:*:*:user:my_password
saved it and then gave it permissions as follows
sudo chmod 600 .pgpass
I'm not sure if this is relevant but what I have as "usr" in my file path to the .pgpass file is different to my database username; what I have here as "user". Also the script I am running is in a completely different directory on the server to the .pgpass file. These are all novice points i'm sure but for the sake of being complete I thought I'd add them. If there was a way to modify the existing script so that it didn't prompt me for a password that would be great, otherwise if anyone has any guidance on what I might be doing wrong with the .pgpass file I'd appreciate it. Thanks in advance