0

I have been going through topics, but I cannot seem to find out why my code does not work.

What I am trying to do is run PSQL command line from Task Scheduler on a Windows machine. This PSQL should open a local file and execute the DROP TABLE commands in it.

The .sql file contains:

drop table public."ORDERS";
drop table public."CONSIGNMENTS";

The task scheduler is set like this: image of Task Scheduler

And the argument in task scheduler tries the following code on one line:

psql -U postgres -h localhost -p 5433 << 'EOF' \c SoloSynch \i C:\Users\bckadmin\Desktop\drop_solosynch_tables.sql EOF

Problem is I do not get anything as a feedback. I just check in the DB and the tables are still present. Where am I going wrong?

EDIT: more codes I have tried and do not work. Nothing I seem to try from looking at other answers works...

psql -h localhost -U postgres -d SoloSynch -p 5433 -c 'drop table public."ASENDUNG";'

psql -h localhost -U postgres -d SoloSynch -p 5433 -c "drop table public."ASENDUNG";"

psql -h localhost -U postgres -d SoloSynch -p 5433 -c "drop table public."ASENDUNG";";

PGPASSWORD=passpsql psql -h localhost -U postgres -d SoloSynch -p 5433 -c "drop table public."ASENDUNG";"
    
PGPASSWORD=pass psql -h localhost -U postgres -p 5433 -d SoloSynch -c "drop table public."ASENDUNG";";
    
PGPASSWORD=pass psql -h localhost -U postgres -p 5433 -d SoloSynch -a -q -f C:\Users\bckadmin\Desktop\drop_solosynch_tables.sql

EDIT2: I do not know if this is relevant, but when I open pgAdmin 4 i enter password 2 times. Upon start and then for the DB. When I open manually psql i need only 1 time the password.

DarkBlade
  • 47
  • 6

1 Answers1

0

Found a workaround.

  1. Created a User variable in Environment Variables window for PGPASSWORD;
  2. Created a .bat file to run from cmd:

@echo off "C:/Users/somebody/Desktop/drop_solocopy_tables.sql" -d SoloCopypsql -h localhost -U postgres -p 5432 -f

  1. In task scheduler in Actions:

Start a program;

Program/script: C:\scripts\psql_DROP_TABLES_SoloCopy.bat

Start in: C:\scripts\

This starts the .bat file through cmd, and cmd starts psql. The PGPASSWORD variable is set as a User variable so that I do not need to call it manually each time. This is a security risk! The password is visible to anyone. Another possible way to set the PGPASSWORD might be to include the following code into the .bat file:

@echo off
**set PGPASSWORD=postgres**
psql -h localhost -U postgres -p 5432 -f "C:/Users/somebody/Desktop/drop_solocopy_tables.sql" -d SoloCopy
DarkBlade
  • 47
  • 6