为每个客户端提供Saperate PostgreSQL数据库,并在单个Django应用和同一服务器上创建客户端时自动迁移 [英] Saperate PostgreSQL db for each client, with automated migrations on creating client on single Django app and on same server

查看:87
本文介绍了为每个客户端提供Saperate PostgreSQL数据库,并在单个Django应用和同一服务器上创建客户端时自动迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

client_obj = Client.objects.create(name='client1')

status = create_database('client1')

def create_database('client1'):
    con = None
    dbname = 'client1'
    con = connect(dbname='postgres', user='***', host = 
    'localhost', password='***')
    con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
    cur = con.cursor()
    cur.execute("SELECT 1 FROM pg_catalog.pg_database WHERE 
    datname = '{}' ".format(dbname))

    exists = cur.fetchone()
    if not exists:
        cur.execute('CREATE DATABASE ' + dbname)
        print "DATABASE NOT EXISTS"
    else:
        print "DATABASE EXISTS"

 cur.close()
 con.close()

创建数据库后如何使迁移自动化?还是有另一种方法来实现此目的?

How to make migrations automated once the db has been created? Or is there another way to accomplish this?

推荐答案

而不是使用:

cur.execute('CREATE DATABASE ' + dbname)

我使用初始迁移创建了一个空数据库,并使用以下命令每次复制

I created an empty database with initial migrations and copy it each time by using the command:

cur.execute('CREATE DATABASE {} WITH TEMPLATE created_db'.format(dbname))

现在不需要动态迁移到新创建的数据库。

Now there no need of dynamic migrations to the newly created db.

这篇关于为每个客户端提供Saperate PostgreSQL数据库,并在单个Django应用和同一服务器上创建客户端时自动迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆