PostgreSQL – How to change default schema
“public” is PostgreSQL default scheme, i have to change it because i had migrated a new database data into another new schema call “new_public”.
Before start to change, i have to check what is current PostgreSQL default schema?
1) Command
SHOW search_path2) Check postgresql.conf
#--------------------------------------------------------------------------- # CLIENT CONNECTION DEFAULTS #--------------------------------------------------------------------------- # - Statement Behavior - #search_path = '"$user",public' # schema names #default_tablespace = '' # a tablespace name, '' uses # the DEFAULT #check_function_bodies = ON #default_transaction_isolation = 'read committed' #default_transaction_read_only = off
Here i show how to change Postgresql default schema.
SET search_path = new_schema
However above command is apply to current session only, next time schema will change back to public. If we want to make effect permanently, we have to change in postgresql.conf file like following.
#--------------------------------------------------------------------------- # CLIENT CONNECTION DEFAULTS #--------------------------------------------------------------------------- # - Statement Behavior - #search_path = '"$user",public' # schema names search_path = '"$user",new_schema' # NEW SCHEMA HERE #default_tablespace = '' # a tablespace name, '' uses # the DEFAULT #check_function_bodies = ON #default_transaction_isolation = 'read committed' #default_transaction_read_only = off
After that just restart PostgreSQL service. Done.
[...] Others recommend setting the search_path in the postgresql.conf. Current versions of PostgreSQL can set the search_path permanently on a per-database basis without having to touch system configuration files: [...]
Hi,
can you please explain me how to change default schema in a per-database configuration.
I’ve the same user enabled to connect to different databases and i’d like to dinamically set e default schema for each database.
Thanks
Mauriizo
Hi,
I have some different issue. I have created a Database, “DB1″ which has more than 1 schema, viz, “Common”, “User” and ofcourse “public”. I have created a LoginRole also, “meUser”.
1. Created DB1, Common, User and tables under Common/User with owner=meUser
2. Given Privileges for all tables inside Common/User to public as well as meUser.
3. Given Priv. and Grants for Schema Common/User to meUser
4. Given Priv. and Grants for DB1 to meUser
But now when I use pgAdmin3 to exe “select * from Common.myTable” I get the following error.
ERROR: schema “Common” does not exist
********** Error **********
ERROR: schema “Common” does not exist
SQL state: 3F000
I know I have not done something correct in Priv./Grants section because of which this error is coming. But I am not able to figure this out.
Can you help me, in suggesting what might have gone wrong and how can I get this working?
Atul
Postgres use lower case for all not-quoted fields on the sql so try using quotes for the schema name i.e. “Common”.table
Hope it helps.