0

In psql, \i can be used to read and execute a sql script. But I have to specify a pathname to the script file to \i, which is very inconvenient, when pathname is long and differs from script to script.

Is there something similar to $PATH in bash for psql's \i to search for a script based only on the filename of a sql script?

Or do I really have to specify a pathname to \i every time I run a sql script file?

Thanks.

I am trying to place https://stackoverflow.com/a/771880/156458 into a sql script, so that I can reuse it.

Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

1

Something akin to Oracle's $SQLPATH. I haven't found it. With Oracle it was handy so that I could define commands into separate SQL files and have them available in sqlplus.

With PostgreSQL and psql, the only suggestion I received was to put any commands as aliases in ~/.psqlrc.

Don Seiler
  • 460
  • 5
  • 16
  • Thanks. Can you give an example in ~/.psqlrc, if I have multiple scripts under `/path/to/sqlscripts/`? – Tim Jun 15 '18 at 20:19
  • No, I mean you'd literally have all of the SQL commands as aliases in `~/.psqlrc`. One big file. It's not pretty. – Don Seiler Jun 15 '18 at 20:29
  • Possible to write a loop over all the files under the directory, and assign the filename as the alias to each file? I can do that in bash, but not sure if it can be done in psql. – Tim Jun 15 '18 at 20:48