3

THis doesn't work, resulting in ERROR: relation "user" does not exist

select * from "user"

This one does

select * from "dbo"."user"
5
  • That suggests your search_path does not include the dbo schema. What's the output of SHOW search_path; ? Commented Sep 21, 2015 at 5:10
  • @CraigRinger it shows ""$user",public" Commented Sep 21, 2015 at 5:19
  • ... and are you connected as user dbo? Commented Sep 21, 2015 at 5:19
  • @CraigRinger as user postgres Commented Sep 21, 2015 at 5:21
  • So it's not working because it's not on the search_path. So you have to schema-qualify it. That's the point of having schemas. Commented Sep 21, 2015 at 5:22

1 Answer 1

3

Tables that aren't schema-qualified are searched for on the search_path. This doesn't search every schema. By default it only searches:

  • pg_catalog (implicitly always first, not listed on search_path)
  • pg_temp tablespaces (implicit, not listed on search_path)
  • A schema named after the current user, listed as $user in search_path
  • The public schema

If you want PostgreSQL to look elsewhere you have to modify the search_path to tell it so, or fully schema-qualify the name.

See the PostgreSQL manual on search_path.

Also, note that unlike some databases the name dbo has no particular significance in PostgreSQL. It's just another name.

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

3 Comments

I notice that if a view uses a table whose schema is not on the search path (say, removed after creating the view), Postgres actually returns data. When you try to run the select itself outside that view, the query fails. That happens after removing a schema from the search path.. Re-adding the schema to the view gets lost after creating the view. Annoying.
@RTD That's because the view's search_path is resolved at view creation time and "baked in" to the view. It's good practice to explicitly qualify your references in views and functions, rather than relying on the search path.
I remember I explicitly hardcoded the schema name in the view when creating it. After saving, Postgres modified the view in its own way, which removed the schema name from the view definition since it was in the search_path. However, after removing the schema from the search path, it looked like the view wasn't refactored... I unfortunately don't have time to test this hypothesis, thank you for the reply.

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.