0

I am trying to backup the functions in database using the method described here How to take backup of functions only in Postgres. But it is showing error << was unexpected at this time.

The code I typed in the command prompt is given below.

psql.exe -U postgres -At dbname > /path/to/output/file.sql <<"__END__"
SELECT pg_get_functiondef(f.oid)
FROM pg_catalog.pg_proc f
INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
WHERE n.nspname = 'public';
__END__

Please help.

5
  • I don't think you want the quotes around "__END__" Commented May 26, 2014 at 12:16
  • @Loopo It is showing the same error message. Commented May 26, 2014 at 12:20
  • also, you could try adding -c just in front of your input redirection ... so ...file.sql -c <<__END__ .... Commented May 26, 2014 at 12:29
  • @Loopo Still not working. Commented May 26, 2014 at 12:33
  • just put it all on one line and use -c: psql.exe -U postgres -At dbname -c "your multi-line sql as one line" > /path/to/output/file.sql Commented May 26, 2014 at 12:55

2 Answers 2

0

This is a shell problem.

<< is a feature of Unix-like shells, while you seem to be using Windows.

Just save the text of the query into a file and run it with the -f flag.

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

1 Comment

I placed the query in a text file and used psql -U postgres -d databasename -f D:\q uery.sql >D:/file.sql
0

I'm sure there's a way to do it, but the simplest thing is just supply the sql as one line with the -c option:

psql.exe -U postgres -At dbname -c "SELECT pg_get_functiondef(f.oid) FROM pg_catalog.pg_proc f INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid) WHERE n.nspname = 'public'" > /path/to/output/file.sql

This is also more portable and will work with tools that need one line, like cron etc

Comments

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.