Generate Postgresql Server backup with password via command line

Asked

Viewed 4,035 times

1

I need to perform an automatic Backup of a Postgresql server, I have a command that makes this backup but it always asks me the password and I could not find a command where I can already determine the password of the bank in the proper Script. The command I possess is:

pg_dump.exe --host 127.0.0.1 --port 5432 --username postgres --format Custom --file C: Backup Backup_data.

What I need is a command where I already set the password, so that when the command is executed I do not need to manually enter the password of the bank. If you have any ideas.

2 answers

4


You can use the variables below to facilitate the understanding of the code:

REM IP do Servidor do PostgreSQL:
SET PGHOST=localhost

REM Porta de acesso ao PostgreSQL:
SET PGPORT=5432

REM Base de Dados que será feito backup:
SET PGDATABASE=dbname

REM Usuário da base de dados:
SET PGUSER=user

REM Senha da base de dados
SET PGPASSWORD=pass

REM Diretório de destino do arquivo de Backup:
SET DESTDIR=D:\Backup\

pg_dump.exe -F c -b -v -f %DESTDIR%_%PGDATABASE%.backup

If you don’t want to use the above variables, add the following line before your backup command that you already solve:

SET PGPASSWORD=pass

1

Create a text file '.pgpass' in the user folder with the line "hostname:port:database:username:password". P. ex.:

127.0.0.1:5432:<nome_database>:postgres:<password>

and in the pg_dump command add the option -w or --no-password If you want to use another file and folder name, put the full path in an operating system environment variable named after PGPASSFILE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.