I want to load some SQL functions in a empty database through psql:
psql -d my_database -f fuctions.sql --set ON_ERROR_STOP=1
I use --set ON_ERROR_STOP=1
because I want that psql fails if the script contains errors.
The content of functions.sql
is:
CREATE or REPLACE FUNCTION test_function() RETURNS INT AS $$
SELECT id from test_table;
$$ LANGUAGE sql;
My problem is, that psql checks if test_table
exists when loading function and fails with this error:
ERROR: relation "test_table" does not exist LINE 2: SELECT id from test_table;
But I don't want that psql check if the table exists because I will create this table later.
Following workarounds would work but I cannot use them:
- Ignore errors. I want psql to exit with an error if the script contains i.e. sql syntax errors.
- Use plpgsql functions instead of sql. Of course I could, but simple sql functions are often the best choice.
- Create the table first. My real scenario is actually more complex than this example.