0

Sorry if my question sounds a little bit obvious but I'm a little bit new on creating batch files, so the problem is the next one:

I'm creating a batch file that would to be open CMD and then go to a specific folder (psql) in order to execute some commands witch allow me to load data to a DataBase from a file (.csv)

This is my try:

cd "C:\pgsql\bin" 

psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext@suggestedorder -c "\copy planning.prueba (centro, almacen, fecha_carga)from 'C:\Users\geradiaz.MODELO\Desktop\Envase\Selección_Envase\Inputs\No_Seleccionado\o.csv' with delimiter as ','    

MyPassword

As I've said the first line goes to pgsql folder, the second line execute the command to copy the data to the data base and the last one (MyPassword) supposes to would be writing my password in the next line when CMD ask for it, but it seems like this last line does not work, when I execute the batch it just keeps the screen with the message "Password for User...".

Do you know guys with kind of commands do I need to execute this batch file?

Xkid
  • 338
  • 1
  • 4
  • 17
  • Perhaps [this answer](https://stackoverflow.com/a/6405162/2128947) may be of assistance. – Magoo Jan 28 '21 at 23:36
  • Thanks! that is a great option, and actually I made the file with the specifications that are in that post but I'm not pretty sure how to call it in CMD, do you know how? – Xkid Jan 28 '21 at 23:41
  • AT a guess (from the response below that link) `set "PGPASSWORD=yourpassword"&pgsql -h ...` Kind of defeats the purpose of a password for anyone who can read `batch` - actually just read - but the option's there (apparently - I know nothing about Postgres beyond "it exists") – Magoo Jan 28 '21 at 23:52

1 Answers1

0

There is no way to pass the password as an argument in psql, but you can try using the PGPASSWORD environment variable.

The script would be something like this:

@echo off
C:
cd \pgsql\bin
set PGPASSWORD=MyPassword
psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext@suggestedorder -c ....

For further info, see here: https://www.postgresql.org/docs/9.0/libpq-envars.html

Compo
  • 36,585
  • 5
  • 27
  • 39