A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38542

Issue with manually giving password every time with psql

We are trying to migrate data from one Amazon RDS database to Amazon Aurora Serverless database using psql of postgresql by COPY command. The script is working fine when I run it from an EC2 instance but I need to give password for rdswizard and postgres every iteration manually. I just want to give the password along with my psql command. How to give password along with the psql command not manually every time?

allSites=(3 5 9 11 29 30 31 32 33 34 37 38 39 40 41 45 46 47 48)

for i in "${allSites[@]}"
do
   psql \
    -X \
    -U rdswizard \
    -h my_rds_host_url_goes_here \
    -d wizard \
    -c "\\copy (select site_id,name,phone from client_${i} where date(create_date) > '2019-09-11' LIMIT 100) to stdout" \
| \
psql \
    -X \
    -U postgres \
    -h my_aurora_serverless_host_url_goes_here \
    -d wizard \
    -c "\\copy client_${i}(site_id,name,phone) from stdin"
done

Both of my database host is on remote server not in local machine

Upvotes: 0

Views: 284

Answers (1)

cskuntal10
cskuntal10

Reputation: 66

You can add details to ~/.pgpass file to avoid regularly having to type in passwords. Make sure to provide -rw-------(0600) permissions to the file.

This file should contain lines of the following format:

hostname:port:database:username:password

The password field from the first line that matches the current connection parameters will be used. Refer official documentation

Upvotes: 2

Related Questions