0

I am trying to execute this

psql "user=postgres password=xxxx hostaddr=xxxx port=5432 dbname=xxx options='-f D:\scripts2\201804130713_test.sql'"

programmatically in order to run a change script on my database.

I am following the answer here that has 305 up-votes. How do I specify a password to psql non-interactively? for using the connection string.

The connection string works, but the file argument has all the slashes removed from the path. I don't know how to escape them, and various things I have tried has introduced problems with single quotes, etc. I think my approach may be off by quite bit. I just need a way to do this.

Danny Ellis Jr.
  • 1,674
  • 2
  • 23
  • 38

1 Answers1

0

options in a connection string is for server-side parameters, not for psql command-line switches.

options

Specifies command-line options to send to the server at connection start. For example, setting this to -c geqo=off sets the session's value of the geqo parameter to off. Spaces within this string are considered to separate command-line arguments, unless escaped with a backslash (); write \ to represent a literal backslash. For a detailed discussion of the available options, consult Chapter 19.

You probably want:

psql -f D:\scripts2\201804130713_test.sql "user=postgres password=xxxx hostaddr=xxxx port=5432 dbname=xxx"
Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
  • I tried it and it strips off the slashes. I also tried wrapping it in single quotes and it reverses the slashes, and it does the same thing if I escape the slashes. – Danny Ellis Jr. Apr 13 '18 at 16:47
  • I found a way around it and just went back to regular command-line arguments following a set PGPassword=. Not the way I wanted to do it, but it works. – Danny Ellis Jr. Apr 13 '18 at 16:48