How to pass in password to pg_dump?

后端 未结 16 760
深忆病人
深忆病人 2020-11-28 00:58

I\'m trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs:

0          


        
相关标签:
16条回答
  • 2020-11-28 01:32

    the easiest way in my opinion, this: you edit you main postgres config file: pg_hba.conf there you have to add the following line:

    host <you_db_name> <you_db_owner> 127.0.0.1/32 trust

    and after this you need start you cron thus:

    pg_dump -h 127.0.0.1 -U <you_db_user> <you_db_name> | gzip > /backup/db/$(date +%Y-%m-%d).psql.gz

    and it worked without password

    0 讨论(0)
  • 2020-11-28 01:34

    If you want to do it in one command:

    PGPASSWORD="mypass" pg_dump mydb > mydb.dump
    
    0 讨论(0)
  • 2020-11-28 01:34

    Another (probably not secure) way to pass password is using input redirection i.e. calling

    pg_dump [params] < [path to file containing password]

    0 讨论(0)
  • 2020-11-28 01:37

    Create a .pgpass file in the home directory of the account that pg_dump will run as.

    The format is:

    hostname:port:database:username:password
    

    Then, set the file's mode to 0600. Otherwise, it will be ignored.

    chmod 600 ~/.pgpass
    

    See the Postgresql documentation libpq-pgpass for more details.

    0 讨论(0)
提交回复
热议问题