1

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

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I'm using Linux and this worked perfectly. No backquotes/backticks. Also it's type command not cat see Windows Alternative to cat command
@ArchNoob You provided the link to exactly that question where I found info about the Windows alternative for POSIX cat :o) Look at the comments for the accepted answer (about unnecessary lines) and to the next answer with copy /b. Good luck.
It surely is quite a thread to read but yeah I've seen the problems with type in it. Thanks for correction and the good luck man.
2

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

5 Comments

"wrapper that contains the` \i` commands" you mean the wrapper that contains the functions filenames? Or contains \i filename.sql \n \i filaname2.sql ?
The latter; use a file that includes other files.
Oh, kinda like what Abelisto said in his answer? cat ing the contents of the function files. I wouldn't have known that trick. Thanks. :D
@ArchNoob Yes, it is yet another alternative: ls ./functions/*.sql | sed -e 's/^/\\i /g' > wrapper.sql But we are talking not about PostgreSQL but about shell :o)
Sure, I could tell. Someone in irc (ningu) got me to something like cat ./functions/* | psql ... but I wanted something more psql-ish .. @Abelisto ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.