ArchNoob
ArchNoob

Reputation: 4126

How to source multiple functions in psql?

I have a directory with at least 6 function files.

I'm using psql and I need to be able to source (initialize ?) all function files at once.

I'm sure making a single function and call all others like SELECT fn1, fn2 isn't going to work.

Doing \i functions_folder/fn1.sql 6 times isn't ideal either.

So is there a way I can (maybe) \i functions/*.sql? Doing that currently gives me

functions/*.sql: No such file or directory

I'm using psql on postgresql 9.6.2

Upvotes: 1

Views: 417

Answers (2)

Abelisto
Abelisto

Reputation: 15624

If you are using *nix OS:

postgres=# \! cat ./functions/*.sql > all_functions.sql
postgres=# \i all_functions.sql

For Windows try to find the analogue of the cat (as I remember it is copy command with some flags)

PS I have the feeling that it could be done by using backquotes:

Within an argument, text that is enclosed in backquotes (`) is taken as a command line that is passed to the shell. The output of the command (with any trailing newline removed) replaces the backquoted text.

Documentation

But still have no idea how. Probably somebody more experienced will provide a hint...

Upvotes: 2

Craig Ringer
Craig Ringer

Reputation: 324771

Create a wrapper that contains the \i commands, and \i the wrapper.

Upvotes: 2

Related Questions