python创建postgresql数据库
方式1、
-
from sqlalchemy import create_engine
-
from sqlalchemy_utils import database_exists, create_database
-
-
# postgresql 创建数据库
-
-
user= data["account"]
-
pwd = data["pwd"]
-
host ="w-test.pg.rds.aliyuncs.com"
-
port = 5432
-
-
-
url = 'postgresql://{}:{}@{}:{}/{}'
-
url = url.format(account, pwd, host, port, data["DBName"])
-
-
engine = create_engine(url)
-
if not database_exists(engine.url):
-
create_database(engine.url)
-
-
print(database_exists(engine.url))
方式2、
-
import psycopg2
-
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
-
-
user = "haha"
-
pwd = "123456"
-
port = 5432
-
host = "w-test.pg.rds.aliyuncs.com"
-
db_name = "wahaha"
-
-
conn = psycopg2.connect(database="postgres", port=port, host=host, user=user, password=pwd)
-
-
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
-
-
cursor = conn.cursor()
-
-
# 创建数据库
-
cursor.execute("CREATE DATABASE {}".format(db_name))
-
-

浙公网安备 33010602011771号