I’m trying to do this in a SQL script that I feed into psql:
ALTER DATABASE dbname SET SEARCH_PATH TO myschema,public
but I need dbname to be dynamically set to the current database rather than hard coded.
Is this possible in PostgreSQL? I tried this but it doesn’t work:
ALTER DATABASE (select current_database()) SET SEARCH_PATH TO myschema,public;
You cannot execute such a statement in plain SQL, where identifiers cannot be parametrized.
You could write a plpgsql function to execute dynamic SQL.
With PostgreSQL 9.0 or later you can also use a
DOstatement: