In the Postgres database, the application data can be organized in various ways using Postgres schemas. In the Postgres database cluster, whenever we create a new database, It gets created with the default schema called public schema. This blog post will discuss the Public Schema Security upgrade in PostgreSQL 15.

When we create a table without specifying the schema name, it gets created in the schema, which is set as per the search_path. By default, The first part of search_path defines the schema with the same name as the current user, and the second part of search_path refers to the public schema

Postgres looks for the schemas sequentially according to the list mentioned in the search_path, so when we execute create table command, Postgres creates the table in the first schema mentioned in the search_path. If it is not present, it creates it in the following schema.

Similarly, if the schema name is not specified in the select query, Postgres will search for tables within the named schema according to the search_path.

Public Schema security upgrade in PostgreSQL 15

Up to Postgres 14, whenever you create a database user, by default, it gets created with CREATE and USAGE privileges on the public schema.

It means that until Postgres 14, any user can write to the public schema until you manually revoke the user’s create privilege on the public schema. 

Starting with PostgreSQL 15, the CREATE privilege on public schema is revoked/removed from all users except the database owner. 

In Postgres 15, now new users cannot create tables or write data to Postgres public schema by default. You have to grant create privilege to the new user manually. 

The usage privilege on the public schema for the new users is still present in Postgres 15, like in Postgres 14 and previous versions.

The example below shows that a new user (test1) can create a table in Postgres 14 without granting any privileges.

The example below shows that Postgres 15 only allows new users (test1) to create tables by granting them create privileges on the public schema.

The following example shows that the usage privilege on the public schema for the new users is still present in Postgres 15, like in Postgres 14 and previous versions.

Public schema ownership changes in PostgreSQL 15

In Postgres 14 and previous versions, by default, the public schema is owned by the bootstrap superuser (postgres), but from Postgres 15, ownership of the public schema has been changed to the new pg_database_owner role. It enables every database owner to own the database’s public schema. 

The below example shows the ownership changes between Postgres 14 and Postgres 15.

Postgres 14

Postgres 15

Visit the links below for further details about the Postgres schemas.

https://www.postgresql.org/docs/15/ddl-schemas.html

https://www.postgresql.org/docs/release/15.0/

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together.

 

Download Percona Distribution for PostgreSQL Today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments