3

As I'm sure you will be aware after reading this question, I'm brand new to PowerShell.

I want to back up a PostgreSQL database every day automatically using the Windows task scheduler. The backup file needs to include the current date. I created a file named db_backup.bat and run it within the Task Scheduler. Here it is, broken into several lines for readability:

"C:/Program Files (x86)/PostgreSQL/9.3/bin\pg_dump.exe" 
--host localhost --port 5432 --username "postgres" 
--no-password  --format custom --verbose 
--file "c:/misc/restore_test_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.backup" "Restore Test"

I hate the need to format the date like this. PowerShell has much cleaner ways to extract date information, so I'd like to do this from a PowerShell script.

So I created db_backup.ps1, containing this:

"C:/Program Files (x86)/PostgreSQL/9.3/bin/pg_dump.exe" --host localhost --port 5432 
--username postgres --no-password  --format custom --verbose 
--file "c:/misc/restore_test.backup" "Restore Test" >> c:/misc/restore.txt

When I run this from Powershell, I get an error complaining about an unexpected token named --host. PS thinks "host" is a parameter for PowerShell, but it's supposed to be a parameter for the pg_dump program.

I tried wrapping all of the pg_dump parameters into a single string, but then PS thinks the entire string is a parameter for itself instead of passing it as a parameter to pg_dump.

So, how do I send command-line parameters to pg_dump?

5
  • 3
    See this for running an executable from PowerShell with arguments. • PowerShell: Running Executables Commented Mar 5, 2021 at 17:26
  • Thanks very much! Commented Mar 5, 2021 at 19:21
  • No worries. I see this kind of question a lot on several PowerShell community sites/forums followed. There are a few more articles like that one. Commented Mar 5, 2021 at 19:43
  • Could you post your solution as an answer to this question? It would increase the value of the question. I am new to powershell too, but the link above doesn't ring a bell to me :( Commented Apr 26, 2022 at 9:06
  • Put a & in the front. Commented Apr 16, 2024 at 14:04

1 Answer 1

0

Here is how I do it in my ps1 script to have backups in the format postgres_yyyymmdd.backup:

# Set the path to pg_dump.exe
$pgPath = "C:\Users\Username\AppData\Local\Programs\pgAdmin 4\runtime\"

# Set other parameters
$pgHost = "127.0.0.1"
$port = "5432"
$username = "postgres"
$database = "test_db"
$encoding = "UTF-8"
$currentDate = Get-Date -Format "yyyyMMdd"
$outputFile = "C:\backups\postgres_$currentDate.backup"

# Run the command
cd $pgPath
.\pg_dump.exe --file $outputFile --host $pgHost --port $port --username         $username --no-password --format=c --encoding $encoding --schema-only --verbose $database
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.