How to solve privileges issues when restore PostgreSQL Database

前端 未结 10 1976
Happy的楠姐
Happy的楠姐 2020-12-12 16:19

I have dumped a clean, no owner backup for Postgres Database with the command

pg_dump sample_database -O -c -U

Later, when I restore the databas

相关标签:
10条回答
  • 2020-12-12 16:26

    For people using AWS, the COMMENT ON EXTENSION is possible only as superuser, and as we know by the docs, RDS instances are managed by Amazon. As such, to prevent you from breaking things like replication, your users - even the root user you set up when you create the instance - will not have full superuser privileges:

    http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html

    When you create a DB instance, the master user system account that you create is assigned to the rds_superuser role. The rds_superuser role is a pre-defined Amazon RDS role similar to the PostgreSQL superuser role (customarily named postgres in local instances), but with some restrictions. As with the PostgreSQL superuser role, the rds_superuser role has the most privileges on your DB instance and you should not assign this role to users unless they need the most access to the DB instance.

    In order to fix this error, just use -- to comment out the lines of SQL that contains COMMENT ON EXTENSION

    0 讨论(0)
  • 2020-12-12 16:28

    AWS RDS users if you are getting this it is because you are not a superuser and according to aws documentation you cannot be one. I have found I have to ignore these errors.

    0 讨论(0)
  • 2020-12-12 16:29

    For people who have narrowed down the issue to the COMMENT ON statements (as per various answers below) and who have superuser access to the source database from which the dump file is created, the simplest solution might be to prevent the comments from being included to the dump file in the first place, by removing them from the source database being dumped...

    COMMENT ON EXTENSION postgis IS NULL;
    COMMENT ON EXTENSION plpgsql IS NULL;
    COMMENT ON SCHEMA public IS NULL;
    

    Future dumps then won't include the COMMENT ON statements.

    0 讨论(0)
  • 2020-12-12 16:30

    You can probably safely ignore the error messages in this case. Failing to add a comment to the public schema and installing plpgsql (which should already be installed) aren't going to cause any real problems.

    However, if you want to do a complete re-install you'll need a user with appropriate permissions. That shouldn't be the user your application routinely runs as of course.

    0 讨论(0)
  • 2020-12-12 16:32

    Use the postgres (admin) user to dump the schema, recreate it and grant priviledges for use before you do your restore. In one command:

    sudo -u postgres psql -c "DROP SCHEMA public CASCADE;
    create SCHEMA public;
    grant usage on schema public to public;
    grant create on schema public to public;" myDBName
    
    0 讨论(0)
  • 2020-12-12 16:34

    For people using Google Cloud Platform, any error will stop the import process. Personally I encountered two different errors depending on the pg_dump command I issued :

    1- The input is a PostgreSQL custom-format dump. Use the pg_restore command-line client to restore this dump to a database.

    Occurs when you've tried to dump your DB in a non plain text format. I.e when the command lacks the -Fp or --format=plain parameter. However, if you add it to your command, you may then encounter the following error :

    2- SET SET SET SET SET SET CREATE EXTENSION ERROR: must be owner of extension plpgsql

    This is a permission issue I have been unable to fix using the command provided in the GCP docs, the tips from this current thread, or following advice from Google Postgres team here. Which recommended to issue the following command :

    pg_dump -Fp --no-acl --no-owner -U myusername myDBName > mydump.sql

    The only thing that did the trick in my case was manually editing the dump file and commenting out all commands relating to plpgsql.

    I hope this helps GCP-reliant souls.

    Update :

    It's easier to dump the file commenting out extensions, especially since some dumps can be huge : pg_dump ... | grep -v -E '(CREATE\ EXTENSION|COMMENT\ ON)' > mydump.sql

    Which can be narrowed down to plpgsql : pg_dump ... | grep -v -E '(CREATE\ EXTENSION\ IF\ NOT\ EXISTS\ plpgsql|COMMENT\ ON\ EXTENSION\ plpgsql)' > mydump.sql

    0 讨论(0)
提交回复
热议问题