Django Database Prefix

前端 未结 2 1182
生来不讨喜
生来不讨喜 2021-01-27 16:36

I need to merge two databases for two different apps. How can add prefix to all Django tables to avoid any conflict?

For example, option should look like:



        
2条回答
  •  青春惊慌失措
    2021-01-27 17:22

    An alternative to prefixing all the names is to put one of the two DBs into a different schema (multiple schemas can coexist in the same database, even if the objects have the same names) This will also take care of objects other than tables, such as indexes, views, functions, ...

    So on one of the databases, just do

    ALTER SCHEMA public RENAME TO myname;
    

    After that, you can dump it (pg_dump -n myname to dump only one schema), and import it into the other database, without the chance of collisions.

    You refer to tables or other objects in the new schema by myname.tablname or by setting the search_path (this can be done on a per-user basis eg via ALTER USER SET search_path = myschema, pg_catalog;)


    Note: there may be a problem with frameworks and clients not being schema-aware, so you might need some additional tweaking. YMMV.


    http://www.postgresql.org/docs/9.4/static/sql-alterschema.html

提交回复
热议问题