Generate backup of a PostgreSQL Server with password via command line

0

I need to automatically back up a PostgreSQL server, I have a command that does this backup but it always asks me for the password and I did not find a command where I can already determine the password of the database already in Script . The command I have is:

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

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

    
asked by anonymous 03.01.2017 / 17:04

2 answers

2

You can use the variables below to make it easier to understand 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 do not want to use the variables above, add the following line before your backup command:

SET PGPASSWORD=pass
    
03.01.2017 / 17:10
0

Create a '.pgpass' text file in the user's folder with the line 'hostname: port: database: username: password'. Eg:

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

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

    
27.06.2018 / 16:54