创建数据库用户 (Data Management)

许可等级:BasicStandardAdvanced

摘要

创建数据库用户工具用于在数据库中创建具有足够权限可创建数据的数据库用户。

用法

语法

CreateDatabaseUser_management (input_database, {user_authentication_type}, user_name, {user_password}, {role}, {tablespace_name})
参数说明数据类型
input_database

指定 Oracle、PostgreSQL 或 SQL Server 中数据库或企业级地理数据库的连接文件。请确保连接文件直接连接到数据库(而非 ArcSDE 服务)并且以数据库管理员用户的身份建立连接。连接到 Oracle 时,您必须以系统用户身份进行连接。

Workspace
user_authentication_type
(可选)

仅当要为其创建数据库用户的操作系统登录存在时,才使用此参数。仅适用于 SQL Server 和 Oracle 数据库。

  • DB创建经数据库验证的用户。这是默认设置。 如果 DBMS 没有配置为允许数据库身份验证,则不要使用此选项。
  • OSA创建经操作系统身份验证的用户。相应的登录必须已存在。 如果 DBMS 没有配置为允许操作系统身份验证,则不要使用此选项。
Boolean
user_name

为新数据库用户输入名称。

如果选择为操作系统登录帐户创建数据库用户,则用户名必须与登录名匹配。

String
user_password
(可选)

为新用户输入密码。将强制基础数据库的密码策略。

如果选择为操作系统登录创建数据库用户,则不需要输入。

Encrypted String
role
(可选)

如果要将新用户添加到现有数据库角色,则输入角色名称。

String
tablespace_name
(可选)

在 Oracle 数据库中创建用户时,输入要用作用户的默认表空间的表空间名称。可指定预配置表空间,如果表空间不存在,则将在 Oracle 默认存储位置创建大小设置为 400 MB 的表空间。如果未指定表空间,则用户的默认表空间将设置为 Oracle 默认表空间。

String

代码实例

创建用户 (CreateUser) 示例 1

使用预定义的数据库连接文件 (oracledb1.sde) 在 Oracle 中创建数据库用户并为该用户创建默认表空间 (sdetbs)

#Import arcpy module
import arcpy

CreateDatabaseUser_management("C:\Documents and Settings\user1\Application Data\ESRI\ArcCatalog\oracledb1.sde", "DB", "map", "Pam987", "sdetbs")
创建用户 (CreateUser) 示例 2

在默认 ArcGIS for Desktop 连接位置创建输入工作空间 (pgconn.sde),并在 PostgreSQL 中创建数据库登录角色和方案

#Import arcpy module
import arcpy

CreateDatabaseConnection_management("Database Connections", "pgconn.sde", "POSTGRESQL", myserver, mypgdb, "DATABASE_AUTH", "ela", "3L@pwd", "SAVE_USERNAME")

CreateDatabaseUser_management("C:\Documents and Settings\u5\Application Data\ESRI\ArcCatalog\pgconn.sde", "DB", "dataowner", "N0look")
创建用户 (CreateUser) 示例 3

在 SQL Server 中创建映射到现有操作系统登录用户 (mynet\vorhoos) 的数据库用户,并使用预定义数据库连接文件 (connection_ssi.sde)

#Import arcpy module
import arcpy

CreateDatabaseUser_management("C:\Documents and Settings\u5\Application Data\ESRI\ArcCatalog\connection_ssi.sde", "OSA", "mynet\vorhoos", "", "")
创建用户 (CreateUser) 示例 4

以下独立脚本可以使用选项提供站点特定的信息,从而创建数据库用户:

"""
Name: create_database_user.py
Description: Provide connection information to a database user.
Type create_database_user.py -h or create_database_user.py --help for usage
Author: Esri
"""

# Import system modules
import arcpy, os, optparse, sys


# Define usage and version
parser = optparse.OptionParser(usage = "usage: %prog [Options]", version="%prog 1.0 for 10.1 release")

#Define help and options
parser.add_option ("--DBMS", dest="Database_type", type="choice", choices=['SQLSERVER', 'ORACLE', 'POSTGRESQL', ''], default="", help="Type of enterprise DBMS:  SQLSERVER, ORACLE, or POSTGRESQL.")                   
parser.add_option ("-i", dest="Instance", type="string", default="", help="DBMS instance name")
parser.add_option ("-D", dest="Database", type="string", default="none", help="Database name:  Not required for Oracle")
parser.add_option ("--auth", dest="Account_authentication", type ="choice", choices=['DATABASE_AUTH', 'OPERATING_SYSTEM_AUTH'], default='DATABASE_AUTH', help="Authentication type options (case-sensitive):  DATABASE_AUTH, OPERATING_SYSTEM_AUTH.  Default=DATABASE_AUTH")
parser.add_option ("-U", dest="Dbms_admin", type="string", default="", help="DBMS administrator user")
parser.add_option ("-P", dest="Dbms_admin_pwd", type="string", default="", help="DBMS administrator password")
parser.add_option ("--utype", dest="user_type", type ="choice", choices=['DATABASE_USER', 'OPERATING_SYSTEM_USER'], default='DATABASE_USER', help="Authentication type options (case-sensitive):  DATABASE_USER, OPERATING_SYSTEM_USER.  Default=DATABASE_USER")
parser.add_option ("-u", dest="dbuser", type="string", default="", help="database user name")
parser.add_option ("-p", dest="dbuser_pwd", type="string", default="", help="database user password")
parser.add_option ("-r", dest="role", type="string", default="", help="role to be granted to the user")
parser.add_option ("-t", dest="Tablespace", type="string", default="", help="Tablespace name")
# Check if value entered for option
try:
	(options, args) = parser.parse_args()

	#Check if no system arguments (options) entered
	if len(sys.argv) == 1:
		print "%s: error: %s\n" % (sys.argv[0], "No command options given")
		parser.print_help()
		sys.exit(3)

	#Usage parameters for spatial database connection
	database_type = options.Database_type.upper()
	instance = options.Instance
	database = options.Database.lower()	
	account_authentication = options.Account_authentication.upper()
	dbms_admin = options.Dbms_admin
	dbms_admin_pwd = options.Dbms_admin_pwd
	dbuser = options.dbuser
	dbuser_pwd = options.dbuser_pwd	
	tablespace = options.Tablespace
	user_type = options.user_type
	role = options.role

	
	if (database_type == "SQLSERVER"):
		database_type = "SQL_SERVER"
	
	if( database_type ==""):	
		print " \n%s: error: \n%s\n" % (sys.argv[0], "DBMS type (--DBMS) must be specified.")
		parser.print_help()
		sys.exit(3)		
	
	if(database_type == "SQL_SERVER"):
		if( account_authentication == "DATABASE_AUTH" and dbms_admin == ""):
			print "\n%s: error: %s\n" % (sys.argv[0], "DBMS administrator must be specified with database authentication")
			sys.exit(3)
		if( account_authentication == "OPERATING_SYSTEM_AUTH" and dbms_admin != ""):
			print "\nWarning: %s\n" % ("Ignoring DBMS administrator specified when using operating system authentication...")	
	else:		
		if( dbuser.lower() == ""):
			print "\n%s: error: %s\n" % (sys.argv[0], "Database user must be specified.")
			sys.exit(3)		
		if( dbms_admin == ""):
			print "\n%s: error: %s\n" % (sys.argv[0], "DBMS administrator must be specified!")
			sys.exit(3)

	if ( user_type == "DATABASE_USER" and (dbuser =="" or dbuser_pwd =="")):
		print " \n%s: error: \n%s\n" % (sys.argv[0], "To create database authenticated user, user name and password must be specified!")
		parser.print_help()
		sys.exit(3)	

	# Get the current product license
	product_license=arcpy.ProductInfo()
	
	# Checks required license level
	if product_license.upper() == "ARCVIEW" or product_license.upper() == 'ENGINE':
		print "\n" + product_license + " license found!" + "  Creating a user in an enterprise geodatabase or database requires an ArcGIS for Desktop Standard or Advanced, ArcGIS Engine with the Geodatabase Update extension, or ArcGIS for Server license."
		sys.exit("Re-authorize ArcGIS before creating a database user.")
	else:
		print "\n" + product_license + " license available!  Continuing to create..."
		arcpy.AddMessage("+++++++++")

	# Local variables
	instance_temp = instance.replace("\\","_")
	instance_temp = instance_temp.replace("/","_")
	instance_temp = instance_temp.replace(":","_")
	Conn_File_NameT = instance_temp + "_" + database + "_" + dbms_admin   

	if os.environ.get("TEMP") == None:
		temp = "c:\\temp"	
	else:
		temp = os.environ.get("TEMP")
	
	if os.environ.get("TMP") == None:
		temp = "/usr/tmp"		
	else:
		temp = os.environ.get("TMP")  

	Connection_File_Name = Conn_File_NameT + ".sde"
	Connection_File_Name_full_path = temp + os.sep + Conn_File_NameT + ".sde"
	
	# Check for the .sde file and delete it if present
	arcpy.env.overwriteOutput=True
	if os.path.exists(Connection_File_Name_full_path):
		os.remove(Connection_File_Name_full_path)

	try:
		print "\nCreating Database Connection File...\n"	
		# Process: Create Database Connection File...
		# Usage:  out_file_location, out_file_name, DBMS_TYPE, instnace, database, account_authentication, username, password, save_username_password(must be true)
		#arcpy.CreateDatabaseConnection_management(temp , Connection_File_Name, database_type, instance, database, account_authentication, dbms_admin, dbms_admin_pwd, "TRUE")
		arcpy.CreateDatabaseConnection_management(out_folder_path=temp, out_name=Connection_File_Name, database_platform=database_type, instance=instance, database=database, account_authentication=account_authentication, username=dbms_admin, password=dbms_admin_pwd, save_user_pass="TRUE")
	        for i in range(arcpy.GetMessageCount()):
			if "000565" in arcpy.GetMessage(i):   #Check if database connection was successful
				arcpy.AddReturnMessage(i)
				arcpy.AddMessage("\n+++++++++")
				arcpy.AddMessage("Exiting!!")
				arcpy.AddMessage("+++++++++\n")
				sys.exit(3)            
			else:
				arcpy.AddReturnMessage(i)
				arcpy.AddMessage("+++++++++\n")

		print "Creating database user...\n"
		arcpy.CreateDatabaseUser_management(input_workspace=Connection_File_Name_full_path, user_authentication_type=user_type, user_name=dbuser, user_password=dbuser_pwd, role=role, tablespace_name=tablespace)
		for i in range(arcpy.GetMessageCount()):
			arcpy.AddReturnMessage(i)
		arcpy.AddMessage("+++++++++\n")
	except:
		for i in range(arcpy.GetMessageCount()):
			arcpy.AddReturnMessage(i)
			
#Check if no value entered for option	
except SystemExit as e:
	if e.code == 2:
		parser.usage = ""
		print "\n"
		parser.print_help()   
		parser.exit(2)

环境

此工具不使用任何地理处理环境

相关主题

许可信息

ArcGIS for Desktop Basic: 否
ArcGIS for Desktop Standard: 是
ArcGIS for Desktop Advanced: 是
5/10/2014