使用PostgreSQL创建、连接企业型地理数据库(完整版)

如何实现在ArcGIS创建postgresql的企业型地理数据库呢?本教程以arcgis10.2和postgresql9.0为例。

1、首先得准备好arcgis10.2所能接受型号的postgresql,具体查看官网关于arcgis的配置。

2、由于所有的arcgis是32位系统的,postgressql是64位系统的那么需从官网上下载:libpq.dll、libintl-8.dll、libiconv-2.dll、libeay32.dll、ssleay32.dll这5个文件,并将其粘贴至arcgis的bin目录下。

3、修改pg_hba.conf 文件使之允许数据库集群接受远程连接。我的设置如下:

 在IPV4、IPV6各加入一行代码:host all all  0.0.0.0/0 md5 允许数据库链接并md5加密,所不想加密则将所有的md5改为trust

 

4、将 st_geometry 库复制到 PostgreSQL 安装目录。可以在 ArcGIS 客户端安装目录的 DatabaseSupport 目录中找到与 PostgreSQL 9.0 配合使用的 st_geometry.dll 文件。将 st_geometry.dll 文件从 ArcGIS 客户端移动到 PostgreSQL 服务器上的 PostgreSQL lib 目录。

5、创建数据库、sde用户和方案以及地理数据库:

有几种方法这里以python脚本为例创建,进入Arcdesktop的python窗口输入以下代码:

"""
Name: create_enterprise_gdb.py
Description: Provide connection information to a DBMS instance and create an enterprise geodatabase.
Type  create_enterprise_gdb.py -h or create_enterprise_gdb.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 " + arcpy.GetInstallInfo()['Version'] )

#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 ("--schema", dest="Schema_type", type="choice", choices=['SDE_SCHEMA', 'DBO_SCHEMA'], default="SDE_SCHEMA", help="Schema Type for SQL Server geodatabase, SDE or DBO. Default=SDE_SCHEMA")
parser.add_option ("-u", dest="Gdb_admin", type="string", default="", help="Geodatabase administrator user name")
parser.add_option ("-p", dest="Gdb_admin_pwd", type="string", default="", help="Geodatabase administrator password")
parser.add_option ("-t", dest="Tablespace", type="string", default="", help="Tablespace name")
parser.add_option ("-l", dest="Authorization_file", type="string", default="", help="Full path and name of authorization file")
# 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
    schema_type = options.Schema_type.upper()
    gdb_admin = options.Gdb_admin
    gdb_admin_pwd = options.Gdb_admin_pwd    
    tablespace = options.Tablespace
    license = options.Authorization_file
    
    
    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 (license == ""):
        print " \n%s: error: \n%s\n" % (sys.argv[0], "Authorization file (-l) must be specified.")
        parser.print_help()
        sys.exit(3)            
    
    if(database_type == "SQL_SERVER"):
        if(schema_type == "SDE_SCHEMA" and gdb_admin.lower() != "sde"):
            print "\n%s: error: %s\n" % (sys.argv[0], "To create SDE schema on SQL Server, geodatabase administrator must be SDE.")
            sys.exit(3)
        if (schema_type == "DBO_SCHEMA" and gdb_admin != ""):
            print "\nWarning: %s\n" % ("Ignoring geodatabase administrator specified when creating DBO schema...")
        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 (schema_type == "DBO_SCHEMA"):
            print "\nWarning: %s %s, %s\n" % ("Only SDE schema is supported on", database_type, "switching to SDE schema..." )
            
        if( gdb_admin.lower() == ""):
            print "\n%s: error: %s\n" % (sys.argv[0], "Geodatabase administrator must be specified.")
            sys.exit(3)

        if( gdb_admin.lower() != "sde"):
            if (database_type == "ORACLE"):
                print "\nGeodatabase admin user is not SDE, creating user schema geodatabase on Oracle...\n"
                sys.exit(3)
            else:
                print "\n%s: error: %s for %s.\n" % (sys.argv[0], "Geodatabase administrator must be SDE", database_type)
                sys.exit(3)
            
        if( dbms_admin == ""):
            print "\n%s: error: %s\n" % (sys.argv[0], "DBMS administrator must be specified!")
            sys.exit(3)

        if (account_authentication == "OPERATING_SYSTEM_AUTH"):
            print "Warning: %s %s, %s\n" % ("Only database authentication is supported on", database_type, "switching to database authentication..." )

    # 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 an enterprise geodatabase 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 enterprise geodatabase.")
    else:
        print "\n" + product_license + " license available!  Continuing to create..."
        arcpy.AddMessage("+++++++++")
    
    
    try:
        print "Creating enterprise geodatabase...\n"
        arcpy.CreateEnterpriseGeodatabase_management(database_platform=database_type,instance_name=instance, database_name=database, account_authentication=account_authentication, database_admin=dbms_admin, database_admin_password=dbms_admin_pwd, sde_schema=schema_type, gdb_admin_name=gdb_admin, gdb_admin_password=gdb_admin_pwd, tablespace_name=tablespace, authorization_file=license)
        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)

enter运行,运行后选择Toolbox--Data Management Tools--创建企业级地理数据库:

数据库填充的是在postgres里的数据库名称:默认会有postgres数据库

管理员密码填充安装postgres你设置的密码

地理数据管理员密码随便填一个,但这个密码要记住,因为这是sde角色的登录密码

授权文件是安装Arcserver时使用的密钥。

此时你已经成功创建了企业型地理数据库,接下来连接你的数据库,点击数据库连接--添加数据库连接:

创建企业型数据库本机用户实例填:localhost  局域网内访问用户则填创建企业数据库计算机的IP地址

用户名为sde,密码为之前创建数据库时设置的密码,数据库填你在postgresql中创的数据库。此时,你已经建立好数据库的连接了。接下来postgresql连接你创建企业数据库的本机的方法是:点击文件--添加服务器

名称和主机名填你所要连接到的服务器IP地址,用户名和密码与之前一致。到此本教程就已结束了,如果觉得解决了你的麻烦的请留个赞吧!!!

 

posted @ 2017-07-14 10:09  we_are_young  阅读(2387)  评论(0编辑  收藏  举报