0

If I'd like to save the output from file1.sql to a new file file2.sql, I would use this command in terminal/cmd:

psql -U postgres -f file1.sql -o file2.sql

What if, though, I want file2.sql to be in a different folder?

If I try this command:

psql -U postgres -f file1.sql -o New/file2.sql,

that won't automatically make a new folder and will give an error. The New folder needs to exist before I can do this.

I need to this over many output files and many new folders. One obvious alternative would be to pre-create the required folders using Python, but really, is there no way Postgresql can create folders for me?

1 Answer 1

2

Use mkdir -p.

It will try to create a directory New if it doesn't exist or does nothing if it already exists.The && ensures that your psql command runs only if the mkdir command succeeds.

mkdir -p New && psql -U postgres -f file1.sql -o New/file2.sql

If you want to run os commands inside psql, simply use \! <command> option within file1.sql and then output via \o option.

\! mkdir -p New
\o New/file2.sql
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. For Windows, it seems to work without the -p flag, though.

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.