Could not execute query ошибка роль не существует

Trying to replicate a database from one system to another. The versions involved are 9.5.0 (source) and 9.5.2 (target).

Source db name is foodb with owner pgdba and target db name will be named foodb_dev with owner pgdev.

All commands are run on the target system that will host the replica.

The pg_dump command is:

    pg_dump -f schema_backup.dump --no-owner -Fc -U pgdba -h $PROD_DB_HOSTNAME -p $PROD_DB_PORT -d foodb -s --clean;

This runs without errors.

The corresponding pg_restore is:

    pg_restore --no-owner --if-exists -1 -c -U pgdev -d foodb_dev schema_backup.dump

which throws error:

pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 3969; 0 0 ACL public pgdba
pg_restore: [archiver (db)] could not execute query: ERROR:  role "pgdba" does not exist
Command was: REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM pgdba;
GRANT ALL ON SCHEMA public TO pgdba;
GRANT ...

If I generate the dump file in plain text format (-Fp) I see it includes several entries like:

REVOKE ALL ON TABLE dump_thread FROM PUBLIC;
REVOKE ALL ON TABLE dump_thread FROM pgdba;
GRANT ALL ON TABLE dump_thread TO pgdba;
GRANT SELECT ON TABLE dump_thread TO readonly;

that try to set privileges for user pgdba who of course doesn’t even exist as a user on the target system which only has user pgdev, and thus the errors from pg_restore.

On the source db the privileges for example of the dump_thread table:

# dp+ dump_thread
Access privileges
-[ RECORD 1 ]-----+--------------------
Schema            | public
Name              | dump_thread
Type              | table
Access privileges | pgdba=arwdDxt/pgdba+
                  | readonly=r/pgdba
Column privileges |
Policies          |

A quick solution would be to simply add a user pgdba on the target cluster and be done with it.

But shouldn’t the --no-owner take care of not including owner specific commands in the dump in the first place?

I am trying to restore a database from one schema to another schema in PostgreSQL using pgadmin backup and restore utility.
The backup is successful for the database A but when I try to do the restore of this to another database B I get the following error:

Sample:

pg_restore: creating TABLE "public.app_role"
pg_restore: [archiver (db)] Error from TOC entry 227; 1259 197811 TABLE app_role schedule
pg_restore: [archiver (db)] could not execute query: ERROR:  role "schedule" does not exist
    Command was: ALTER TABLE public.app_role OWNER TO schedule;

I tried changing the restore options like do not save owner as yes, privileges yes etc but still there was no luck.

Does anyone know about this or have faced an issue like this before?

Nisse Engström's user avatar

asked Jul 10, 2019 at 1:02

Sunil Cyriac's user avatar

You need the roles referenced in the backup. To obtain those, do the following:

pg_dumpall --globals-only > globals.dump

Then you can pg_restore -f globals.dump <database>
This will restore all of the roles on the source database cluster.

If you are unable to do that for whatever reason, you can also edit your backup file for references to that schedule role for another that you have setup on your target system

answered Jul 28, 2019 at 13:11

justsomeguy's user avatar

0

pg_restore: connecting to database for restore pg_restore: creating TYPE «public.mpaa_rating» pg_restore: while PROCESSING TOC: pg_restore: from TOC entry 632; 1247 16723 TYPE mpaa_rating postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TYPE public.mpaa_rating OWNER TO postgres; pg_restore: creating DOMAIN «public.year» pg_restore: from TOC entry 635; 1247 16734 DOMAIN year postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER DOMAIN public.year OWNER TO postgres; pg_restore: creating FUNCTION «public._group_concat(text, text)» pg_restore: from TOC entry 231; 1255 16736 FUNCTION _group_concat(text, text) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public._group_concat(text, text) OWNER TO postgres; pg_restore: creating FUNCTION «public.film_in_stock(integer, integer)» pg_restore: from TOC entry 232; 1255 16737 FUNCTION film_in_stock(integer, integer) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.film_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres; pg_restore: creating FUNCTION «public.film_not_in_stock(integer, integer)» pg_restore: from TOC entry 233; 1255 16738 FUNCTION film_not_in_stock(integer, integer) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.film_not_in_stock(p_film_id integer, p_store_id integer, OUT p_film_count integer) OWNER TO postgres; pg_restore: creating FUNCTION «public.get_customer_balance(integer, timestamp without time zone)» pg_restore: from TOC entry 248; 1255 16739 FUNCTION get_customer_balance(integer, timestamp without time zone) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.get_customer_balance(p_customer_id integer, p_effective_date timestamp without time zone) OWNER TO postgres; pg_restore: creating FUNCTION «public.inventory_held_by_customer(integer)» pg_restore: from TOC entry 249; 1255 16740 FUNCTION inventory_held_by_customer(integer) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.inventory_held_by_customer(p_inventory_id integer) OWNER TO postgres; pg_restore: creating FUNCTION «public.inventory_in_stock(integer)» pg_restore: from TOC entry 250; 1255 16741 FUNCTION inventory_in_stock(integer) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.inventory_in_stock(p_inventory_id integer) OWNER TO postgres; pg_restore: creating FUNCTION «public.last_day(timestamp without time zone)» pg_restore: from TOC entry 234; 1255 16742 FUNCTION last_day(timestamp without time zone) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.last_day(timestamp without time zone) OWNER TO postgres; pg_restore: creating FUNCTION «public.last_updated()» pg_restore: from TOC entry 235; 1255 16743 FUNCTION last_updated() postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.last_updated() OWNER TO postgres; pg_restore: creating SEQUENCE «public.customer_customer_id_seq» pg_restore: from TOC entry 196; 1259 16744 SEQUENCE customer_customer_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.customer_customer_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.customer» pg_restore: from TOC entry 197; 1259 16746 TABLE customer postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.customer OWNER TO postgres; pg_restore: creating FUNCTION «public.rewards_report(integer, numeric)» pg_restore: from TOC entry 251; 1255 16753 FUNCTION rewards_report(integer, numeric) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER FUNCTION public.rewards_report(min_monthly_purchases integer, min_dollar_amount_purchased numeric) OWNER TO postgres; pg_restore: creating AGGREGATE «public.group_concat(text)» pg_restore: from TOC entry 721; 1255 16754 AGGREGATE group_concat(text) postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER AGGREGATE public.group_concat(text) OWNER TO postgres; pg_restore: creating SEQUENCE «public.actor_actor_id_seq» pg_restore: from TOC entry 198; 1259 16755 SEQUENCE actor_actor_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.actor_actor_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.actor» pg_restore: from TOC entry 199; 1259 16757 TABLE actor postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.actor OWNER TO postgres; pg_restore: creating SEQUENCE «public.category_category_id_seq» pg_restore: from TOC entry 200; 1259 16762 SEQUENCE category_category_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.category_category_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.category» pg_restore: from TOC entry 201; 1259 16764 TABLE category postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.category OWNER TO postgres; pg_restore: creating SEQUENCE «public.film_film_id_seq» pg_restore: from TOC entry 202; 1259 16769 SEQUENCE film_film_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.film_film_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.film» pg_restore: from TOC entry 203; 1259 16771 TABLE film postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.film OWNER TO postgres; pg_restore: creating TABLE «public.film_actor» pg_restore: from TOC entry 204; 1259 16783 TABLE film_actor postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.film_actor OWNER TO postgres; pg_restore: creating TABLE «public.film_category» pg_restore: from TOC entry 205; 1259 16787 TABLE film_category postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.film_category OWNER TO postgres; pg_restore: creating VIEW «public.actor_info» pg_restore: from TOC entry 206; 1259 16791 VIEW actor_info postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.actor_info OWNER TO postgres; pg_restore: creating SEQUENCE «public.address_address_id_seq» pg_restore: from TOC entry 207; 1259 16796 SEQUENCE address_address_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.address_address_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.address» pg_restore: from TOC entry 208; 1259 16798 TABLE address postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.address OWNER TO postgres; pg_restore: creating SEQUENCE «public.city_city_id_seq» pg_restore: from TOC entry 209; 1259 16803 SEQUENCE city_city_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.city_city_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.city» pg_restore: from TOC entry 210; 1259 16805 TABLE city postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.city OWNER TO postgres; pg_restore: creating SEQUENCE «public.country_country_id_seq» pg_restore: from TOC entry 211; 1259 16810 SEQUENCE country_country_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.country_country_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.country» pg_restore: from TOC entry 212; 1259 16812 TABLE country postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.country OWNER TO postgres; pg_restore: creating VIEW «public.customer_list» pg_restore: from TOC entry 213; 1259 16817 VIEW customer_list postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.customer_list OWNER TO postgres; pg_restore: creating VIEW «public.film_list» pg_restore: from TOC entry 214; 1259 16822 VIEW film_list postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.film_list OWNER TO postgres; pg_restore: creating SEQUENCE «public.inventory_inventory_id_seq» pg_restore: from TOC entry 215; 1259 16827 SEQUENCE inventory_inventory_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.inventory_inventory_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.inventory» pg_restore: from TOC entry 216; 1259 16829 TABLE inventory postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.inventory OWNER TO postgres; pg_restore: creating SEQUENCE «public.language_language_id_seq» pg_restore: from TOC entry 217; 1259 16834 SEQUENCE language_language_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.language_language_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.language» pg_restore: from TOC entry 218; 1259 16836 TABLE language postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.language OWNER TO postgres; pg_restore: creating VIEW «public.nicer_but_slower_film_list» pg_restore: from TOC entry 219; 1259 16841 VIEW nicer_but_slower_film_list postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.nicer_but_slower_film_list OWNER TO postgres; pg_restore: creating SEQUENCE «public.payment_payment_id_seq» pg_restore: from TOC entry 220; 1259 16846 SEQUENCE payment_payment_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.payment_payment_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.payment» pg_restore: from TOC entry 221; 1259 16848 TABLE payment postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.payment OWNER TO postgres; pg_restore: creating SEQUENCE «public.rental_rental_id_seq» pg_restore: from TOC entry 222; 1259 16852 SEQUENCE rental_rental_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.rental_rental_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.rental» pg_restore: from TOC entry 223; 1259 16854 TABLE rental postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.rental OWNER TO postgres; pg_restore: creating VIEW «public.sales_by_film_category» pg_restore: from TOC entry 224; 1259 16859 VIEW sales_by_film_category postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.sales_by_film_category OWNER TO postgres; pg_restore: creating SEQUENCE «public.staff_staff_id_seq» pg_restore: from TOC entry 225; 1259 16864 SEQUENCE staff_staff_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.staff_staff_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.staff» pg_restore: from TOC entry 226; 1259 16866 TABLE staff postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.staff OWNER TO postgres; pg_restore: creating SEQUENCE «public.store_store_id_seq» pg_restore: from TOC entry 227; 1259 16875 SEQUENCE store_store_id_seq postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.store_store_id_seq OWNER TO postgres; pg_restore: creating TABLE «public.store» pg_restore: from TOC entry 228; 1259 16877 TABLE store postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.store OWNER TO postgres; pg_restore: creating VIEW «public.sales_by_store» pg_restore: from TOC entry 229; 1259 16882 VIEW sales_by_store postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.sales_by_store OWNER TO postgres; pg_restore: creating VIEW «public.staff_list» pg_restore: from TOC entry 230; 1259 16887 VIEW staff_list postgres pg_restore: error: could not execute query: ERROR: role «postgres» does not exist Command was: ALTER TABLE public.staff_list OWNER TO postgres; pg_restore: processing data for table «public.actor» pg_restore: processing data for table «public.address» pg_restore: processing data for table «public.category» pg_restore: processing data for table «public.city» pg_restore: processing data for table «public.country» pg_restore: processing data for table «public.customer» pg_restore: processing data for table «public.film» pg_restore: processing data for table «public.film_actor» pg_restore: processing data for table «public.film_category» pg_restore: processing data for table «public.inventory» pg_restore: processing data for table «public.language» pg_restore: processing data for table «public.payment» pg_restore: processing data for table «public.rental» pg_restore: processing data for table «public.staff» pg_restore: processing data for table «public.store» pg_restore: executing SEQUENCE SET actor_actor_id_seq pg_restore: executing SEQUENCE SET address_address_id_seq pg_restore: executing SEQUENCE SET category_category_id_seq pg_restore: executing SEQUENCE SET city_city_id_seq pg_restore: executing SEQUENCE SET country_country_id_seq pg_restore: executing SEQUENCE SET customer_customer_id_seq pg_restore: executing SEQUENCE SET film_film_id_seq pg_restore: executing SEQUENCE SET inventory_inventory_id_seq pg_restore: executing SEQUENCE SET language_language_id_seq pg_restore: executing SEQUENCE SET payment_payment_id_seq pg_restore: executing SEQUENCE SET rental_rental_id_seq pg_restore: executing SEQUENCE SET staff_staff_id_seq pg_restore: executing SEQUENCE SET store_store_id_seq pg_restore: creating CONSTRAINT «public.actor actor_pkey» pg_restore: creating CONSTRAINT «public.address address_pkey» pg_restore: creating CONSTRAINT «public.category category_pkey» pg_restore: creating CONSTRAINT «public.city city_pkey» pg_restore: creating CONSTRAINT «public.country country_pkey» pg_restore: creating CONSTRAINT «public.customer customer_pkey» pg_restore: creating CONSTRAINT «public.film_actor film_actor_pkey» pg_restore: creating CONSTRAINT «public.film_category film_category_pkey» pg_restore: creating CONSTRAINT «public.film film_pkey» pg_restore: creating CONSTRAINT «public.inventory inventory_pkey» pg_restore: creating CONSTRAINT «public.language language_pkey» pg_restore: creating CONSTRAINT «public.payment payment_pkey» pg_restore: creating CONSTRAINT «public.rental rental_pkey» pg_restore: creating CONSTRAINT «public.staff staff_pkey» pg_restore: creating CONSTRAINT «public.store store_pkey» pg_restore: creating INDEX «public.film_fulltext_idx» pg_restore: creating INDEX «public.idx_actor_last_name» pg_restore: creating INDEX «public.idx_fk_address_id» pg_restore: creating INDEX «public.idx_fk_city_id» pg_restore: creating INDEX «public.idx_fk_country_id» pg_restore: creating INDEX «public.idx_fk_customer_id» pg_restore: creating INDEX «public.idx_fk_film_id» pg_restore: creating INDEX «public.idx_fk_inventory_id» pg_restore: creating INDEX «public.idx_fk_language_id» pg_restore: creating INDEX «public.idx_fk_rental_id» pg_restore: creating INDEX «public.idx_fk_staff_id» pg_restore: creating INDEX «public.idx_fk_store_id» pg_restore: creating INDEX «public.idx_last_name» pg_restore: creating INDEX «public.idx_store_id_film_id» pg_restore: creating INDEX «public.idx_title» pg_restore: creating INDEX «public.idx_unq_manager_staff_id» pg_restore: creating INDEX «public.idx_unq_rental_rental_date_inventory_id_customer_id» pg_restore: creating TRIGGER «public.film film_fulltext_trigger» pg_restore: creating TRIGGER «public.actor last_updated» pg_restore: creating TRIGGER «public.address last_updated» pg_restore: creating TRIGGER «public.category last_updated» pg_restore: creating TRIGGER «public.city last_updated» pg_restore: creating TRIGGER «public.country last_updated» pg_restore: creating TRIGGER «public.customer last_updated» pg_restore: creating TRIGGER «public.film last_updated» pg_restore: creating TRIGGER «public.film_actor last_updated» pg_restore: creating TRIGGER «public.film_category last_updated» pg_restore: creating TRIGGER «public.inventory last_updated» pg_restore: creating TRIGGER «public.language last_updated» pg_restore: creating TRIGGER «public.rental last_updated» pg_restore: creating TRIGGER «public.staff last_updated» pg_restore: creating TRIGGER «public.store last_updated» pg_restore: creating FK CONSTRAINT «public.customer customer_address_id_fkey» pg_restore: creating FK CONSTRAINT «public.film_actor film_actor_actor_id_fkey» pg_restore: creating FK CONSTRAINT «public.film_actor film_actor_film_id_fkey» pg_restore: creating FK CONSTRAINT «public.film_category film_category_category_id_fkey» pg_restore: creating FK CONSTRAINT «public.film_category film_category_film_id_fkey» pg_restore: creating FK CONSTRAINT «public.film film_language_id_fkey» pg_restore: creating FK CONSTRAINT «public.address fk_address_city» pg_restore: creating FK CONSTRAINT «public.city fk_city» pg_restore: creating FK CONSTRAINT «public.inventory inventory_film_id_fkey» pg_restore: creating FK CONSTRAINT «public.payment payment_customer_id_fkey» pg_restore: creating FK CONSTRAINT «public.payment payment_rental_id_fkey» pg_restore: creating FK CONSTRAINT «public.payment payment_staff_id_fkey» pg_restore: creating FK CONSTRAINT «public.rental rental_customer_id_fkey» pg_restore: creating FK CONSTRAINT «public.rental rental_inventory_id_fkey» pg_restore: creating FK CONSTRAINT «public.rental rental_staff_id_key» pg_restore: creating FK CONSTRAINT «public.staff staff_address_id_fkey» pg_restore: creating FK CONSTRAINT «public.store store_address_id_fkey» pg_restore: creating FK CONSTRAINT «public.store store_manager_staff_id_fkey» pg_restore: warning: errors ignored on restore: 47

The following commands export data without mixing it with role or privilege data so that you can import it to any role you specify. Also, you can make pg_dump compress the data to a tar file for you.

pg_dump --format=t --blobs --verbose --no-privileges --no-owner 
--password --username mydbuser --database mydbname  
--file "mydbname_dump_$(date +%Y%m%d).tar"

Or a shorter one for it that does the same thing

pg_dump -F t -bvxOW -U myuser -d mydbname  -f "mydbname_dump_$(date +%Y%m%d).tar"

To restore you will then use almost the same flags and also mention which role you want ownership to belong to.

pg_restore -vxOW --role=myuser2 -U myadminuser -d mydbname "mydbname_dump_$(date +%Y%m%d).tar"

Below is what all these flags mean and some more for additional options like compression level or format to output. You can find more by doing pg_dump --help or pg_restore --help:

General options:
  -f, --file=FILENAME          output file or directory name
  -F, --format=c|d|t|p         output file format (custom, directory, tar,
                               plain text (default))
  -v, --verbose                verbose mode
  -Z, --compress=0-9           compression level for compressed formats

Options controlling the output content:
  -b, --blobs                  include large objects in dump
  -O, --no-owner               skip restoration of object ownership in
                               plain-text format
  -x, --no-privileges          do not dump privileges (grant/revoke)

Connection options:
  -d, --dbname=DBNAME      database to dump
  -h, --host=HOSTNAME      database server host or socket directory
  -p, --port=PORT          database server port number
  -U, --username=NAME      connect as specified database user
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)
  --role=ROLENAME          do SET ROLE before dump

Is there a way to export a PostgreSQL database and later import it with another name?

I’m using PostgreSQL with Rails and I often export the data from production, where the database is called blah_production and import it on development or staging with names blah_development and blah_staging. On MySQL this is trivial as the export doesn’t have the database anywhere (except a comment maybe), but on PostgreSQL it seems to be impossible. Is it impossible?

I’m currently dumping the database this way:

pg_dump blah > blah.dump

I’m not using the -c or -C options. That dump contains statements such as:

COMMENT ON DATABASE blah IS 'blah';

ALTER TABLE public.checks OWNER TO blah;

ALTER TABLE public.users OWNER TO blah;

When I try to import with

psql blah_devel < blah.dump

I get

WARNING:  database "blah" does not exist

ERROR:  role "blah" does not exist

Maybe the problem is not really the database but the role?

If I dump it this way:

pg_dump --format=c blah > blah.dump

and try to import it this way:

pg_restore -d blah_devel < tmp/blah.psql

I get these errors:

pg_restore: WARNING:  database "blah" does not exist
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 1513; 1259 16435 TABLE checks blah
pg_restore: [archiver (db)] could not execute query: ERROR:  role "blah" does not exist
    Command was: ALTER TABLE public.checks OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1509; 1259 16409 TABLE users blah
pg_restore: [archiver (db)] could not execute query: ERROR:  role "blah" does not exist
    Command was: ALTER TABLE public.users OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1508; 1259 16407 SEQUENCE users_id_seq blah
pg_restore: [archiver (db)] could not execute query: ERROR:  role "blah" does not exist
    Command was: ALTER TABLE public.users_id_seq OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1824; 0 0 ACL public postgres
pg_restore: [archiver (db)] could not execute query: ERROR:  role "postgres" does not exist
    Command was: REVOKE ALL ON SCHEMA public FROM postgres;
pg_restore: [archiver (db)] could not execute query: ERROR:  role "postgres" does not exist
    Command was: GRANT ALL ON SCHEMA public TO postgres;
WARNING: errors ignored on restore: 11

Any ideas?

I’ve seen out there some people using sed scripts to modify the dump. I’d like to avoid that solution but if there are no alternative I’ll take it. Has anybody wrote a script to alter the dump’s database name ensure no data is ever altered?

Понравилась статья? Поделить с друзьями:
  • Condtrol smart 60 ошибка 302
  • Coreldraw ошибка при импорте файла
  • Condtrol mettro 60 ошибка 308
  • Constant expression required vba ошибка
  • Condtrol mettro 60 ошибка 302