Auto execute psql commands via batch file

psql via command line does not have an option for password. To run a scheduled task using psql is pointless without full automation.

Warning postgres recommends against doing this, and instead use a password file.

I use this script to kick off the psql command in python. But you can execute psql straight from the batch file, just check the %ERRORLEVEL% batch variable from the calling method.

@echo off

REM scheduled task point to .bat files
REM besides we need to make sure we have system variables in place

REM export a password for use with the system (no quotes)
SET PGHOST=host
SET PGDATABASE=database
SET PGUSER=user
SET PGPASSWORD=user

REM execute psql by file, even though echo is off, errors will still show
psql -X --variable=ON_ERROR_STOP= -1 -w -f filename.sql


Author’s Note

Initial md Generated using https://github.com/jsr6720/wordpress-html-scraper-to-md

Original Wordpress categories: [‘DOS’]

Original Wordpress tags: “DOS”, “bat file”, “batch file”, “DOS”, “psql”

Original Wordpress comments: 2 Comments

Significant revisions

tags: 2010, wordpress, txcowboycoder, shell-script, batch-file, psql, postgreSQL

  • May 6th, 2024 Converted to jekyll markdown format and copied to personal site
  • Nov 11th, 2010 Originally published on txcowboycoder wordpress site

EOF/Footnotes