简体   繁体   中英

how can I change the default owner of the public schema on postgresql database?

When I create a database with my user the public schema is created with "postgres" user. I can create and drop tables in this schema but I can't drop the schema or change the owner.

I'm installing Phinx migrations for a php project. This is a mature project which has many tables. For the first migration on the up() method I load a sql file with the schema of the database and on the down() method I would drop schema (tables, triggers, procedures...)

class FirstMigration extends AbstractMigration
{
    public function up()
    {
        $schema= file_get_contents('./database/migrations/schemaDB.sql');
        $this->execute($schema);
    }

    public function down()
    {
        $this->execute("DROP SCHEMA public CASCADE");
        $this->execute("CREATE SCHEMA public");
    }
}

When I do rollback, I have an error:

Insufficient privilege: 7 ERROR: You must be owner of public schema.

图片

When I create the database I choose the user "citest"

图片

But the owner by default for the public schema is "postgres"!

and I can't change it.

My user is the owner of database but isn't the owner of the public schema which is the user "postgres".

The default user of the public schema is set by the template database (called template1 ) that Postgres copies when it creates a new DB. If you want to change defaults of newly created databases you need to modify template1 . For example you can DROP SCHEMA public from template1 . Now you won't need to start your migrations by dropping public because it won't exist. Of course to do this you'll need to (at least temporarily) login with a user that has permissions to edit template1 .

For more information read this thread on the Postgres mailing list, https://www.postgresql.org/message-id/CAFjNrYu8GqD%3D17DimG9QtWoV1hPviqHxaZp2umWAPkpUOFAGYg%40mail.gmail.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM