I have a Django app, the database for which is under active manual development (it’s a language-learning app, so it stores vocabulary, grammatical concepts, etc). I’d prefer to do that development in my local django/postgres environment.
However, I don’t want to be constantly wiping out the User table from the live version!
I’m very new to Postgres, so please don’t assume I know what I’m doing here – would some kind of schema be the right approach here?
To just backup the one table, use
COPYfrom inside the database:or
pg_dumpfrom the shell:Then drop the database, restore your new version, empty
user_tbland useCOPY FROMto restore the one table:or restore the backup with the one table from the shell with
psql:Identify depending tables
Quick and dirty
There is no such thing as “COPY … CASCADE”. The simplest method to identify depending tables would be to start a transaction, call
TRUNCATE tbl CASCADEand record the notices you get:Then roll back the transaction – so nothing actually changes:
Careful with that. If you
COMMITthe truncate goes through.Slow and sure
Well, not actually “slow”, but the code is a lot more complex. This doesn’t take an exclusive lock on the involved tables, though, so it’s a lot cleaner and safer:
Returns:
I use a recursive CTE (requires PostgreSQL 8.4 or later) on the catalog table
pg_constraint, because each table can have dependencies in turn.Use
UNION, notUNION ALLto avoid multiple evaluation of tables that might be linked with multiple foreign keys directly or indirectly.