A UNIQUE
constraint is not a trigger. It is implemented by way of a unique index. So it cannot be turned off with --disable-triggers
.
"Other objects" that depend on a the unique constraint are typically foreign key constraints. Those cannot exist without a unique (or primary key) constraint on the referenced column(s). To enable the restore, you could remove all such fk constraints together with the unique constraint.
Of course, to restore referential integrity, you would then have to eliminate violating duplicates and re-create all removed constraints. If you cannot afford to have an inconsistent state, even temporarily, do it all in a single (automatically blocking) transaction.
If you cannot afford exclusive locks on involved tables, your only remaining option is to fix your data first. This is probably the best course of action either way. You could import your data to temporary tables with COPY
, remove duplicates, and then INSERT
into the target tables.
If, on the other hand, you run into missing values for foreign keys, you can improvise with NOT VALID
fk constraints:
Your problem should never occur to begin with. If you actually have a UNIQUE constraint in place, you cannot have duplicate values in your source database - unless it's seriously broken. If that's the case, fix your source db first ...
The error is harmless but to get rid of it, I think you need to break this restore into two commands, as in:
dropdb -U postgres mydb && \
pg_restore --create --dbname=postgres --username=postgres pg_backup.dump
The --clean
option in pg_restore doesn't look like much but actually raises non-trivial problems.
For versions up to 9.1
The combination of --create
and --clean
in pg_restore options used to be an error in older PG versions (up to 9.1). There is indeed some contradiction between (quoting the 9.1 manpage):
--clean
Clean (drop) database objects before recreating them
and
--create
Create the database before restoring into it.
Because what's the point of cleaning inside a brand-new database?
Starting from version 9.2
The combination is now accepted and the doc says this (quoting the 9.3 manpage):
--clean
Clean (drop) database objects before recreating them. (This might generate some harmless error messages, if any objects were not present in the destination database.)
--create
Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before connecting to it.
Now having both together leads to this kind of sequence during your restore:
DROP DATABASE mydb;
...
CREATE DATABASE mydb WITH TEMPLATE = template0... [other options]
...
CREATE SCHEMA public;
...
CREATE TABLE...
There is no DROP
for each individual object, only a DROP DATABASE
at the beginning. If not using --create
this would be the opposite.
Anyway this sequence raises the error of public
schema already existing because creating mydb
from template0
has imported it already (which is normal, it's the point of a template database).
I'm not sure why this case is not handled automatically by pg_restore
. Maybe this would cause undesirable side-effects when an admin decides to customize template0
and/or change the purpose of public
, even if we're not supposed to do that.
Best Answer
This turned out to be my own fault and not making sure that the Foreman host had a postgresql93 client installed so that the rake task that imports the database could actually do the
pg_restore
on the valid dump.I was able to use
pg_dump -Fp
to get the plain text and validate that my data was in there. Thank you everyone for your help. Chalk this one up to a newbie trying to learn Postgres.Cheers.