Databases, schemas, and search_path
As per the documentation, a database is a named collection of SQL objects. It's not possible to access objects in one database from another database directly. We can access other databases using a database link (or foreign data wrappers), which is an extension, as mentioned in the previous chapter.
Tip
There are two template databases: template0
and template1
in any PostgreSQL cluster. When we create a database using CREATE DATABASE db1;
, a clone of the template1
database is created. If we have a few user-created tables with some master data in template1
, these will be copied to the new database. If we want to create a newdb
database that is a clone of a user-created database mydb
, we can use CREATE DATABASE newdb TEMPLATE mydb;
.
Another important concept is the schema, which is a container or a namespace within a database. Any object that we create in a database (such as a table, an index, a view, and so on) gets created under a schema. We can use...