PostgreSQL: Is it better to use multiple databases with one schema each, or one database with multiple schemas?

后端 未结 5 1996
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 04:06

After this comment to one of my question, I\'m thinking if it is better using one database with X schemas or vice versa.

My situation: I\'m developing a web applicat

5条回答
  •  清歌不尽
    2020-12-02 04:30

    Definitely, I'll go for the one-db-many-schemas approach. This allows me to dump all the database, but restore just one very easily, in many ways:

    1. Dump the db (all the schema), load the dump in a new db, dump just the schema I need, and restore back in the main db.
    2. Dump the schema separately, one by one (but I think the machine will suffer more this way - and I'm expecting like 500 schemas!)

    Otherwise, googling around I've seen that there is no auto-procedure to duplicate a schema (using one as a template), but many suggest this way:

    1. Create a template-schema
    2. When need to duplicate, rename it with new name
    3. Dump it
    4. Rename it back
    5. Restore the dump
    6. The magic is done.

    I've written two rows in Python to do that; I hope they can help someone (in-2-seconds-written-code, don’t use it in production):

    import os
    import sys
    import pg
    
    # Take the new schema name from the second cmd arguments (the first is the filename)
    newSchema = sys.argv[1]
    
    # Temperary folder for the dumps
    dumpFile = '/test/dumps/' + str(newSchema) + '.sql'
    
    # Settings
    db_name = 'db_name'
    db_user = 'db_user'
    db_pass = 'db_pass'
    schema_as_template = 'schema_name'
    
    # Connection
    pgConnect = pg.connect(dbname= db_name, host='localhost', user= db_user, passwd= db_pass)
    
    # Rename schema with the new name
    pgConnect.query("ALTER SCHEMA " + schema_as_template + " RENAME TO " + str(newSchema))
    
    # Dump it
    command = 'export PGPASSWORD="' + db_pass + '" && pg_dump -U ' + db_user + ' -n ' + str(newSchema) + ' ' + db_name + ' > ' + dumpFile
    os.system(command)
    
    # Rename back with its default name
    pgConnect.query("ALTER SCHEMA " + str(newSchema) + " RENAME TO " + schema_as_template)
    
    # Restore the previous dump to create the new schema
    restore = 'export PGPASSWORD="' + db_pass + '" && psql -U ' + db_user + ' -d ' + db_name + ' < ' + dumpFile
    os.system(restore)
    
    # Want to delete the dump file?
    os.remove(dumpFile)
    
    # Close connection
    pgConnect.close()
    

提交回复
热议问题