1

I created a new gdb by

arcpy.CreateFileGDB_management('e:/Rstack', 'new.gdb')
arcpy.env.workspace = 'e:/Rstack/new.gdb'

But how to set this new gdb as my default gdb by arcpy, without opening ArcGIS desktop? Setting env workspace does not seem to work here.

2

2 Answers 2

4

This took a bit of digging, but it annoyed me enough this morning to dig almost all the way down. Unfortunately I didn't find an accessible solution for v10 arcpy, however in Pro it's available out of the box as arcpy.mp.ArcGISProject().defaultGeodatabase

python ArcGIS Pro

    import arcpy
    aprx_path = r'X:\Projects\2019\ENV.533.aprx'
    gdb_path = r'X:\shared\ENV_default.gdb'

    aprx = arcpy.mp.ArcGISProject(aprx_path) # or 'CURRENT' in live py console
    aprx.defaultGeodatabase = gdb_path
    del arcpy.env.workspace # Optional, ensure default gdb used in rest of this script

From https://pro.arcgis.com/en/pro-app/arcpy/mapping/arcgisproject-class.htm

python ArcMap 10

    # Not real code! waiting/digging for solution
    import arcpy.mapping.convertArcObjectToPythonObject as convert
    defgdb = convert('IGxCatalogDefaultDatabase')
    defgdb.DefaultDatabaseName = r"D:\path\to\default.gdb"

From <http://your.kudos.link/here>

C# ArcGIS Pro

    //Create an empty project. The project will be created in the default folder
    //It will be named MyProject1, MyProject2, or similar...
    await Project.CreateAsync();
    //Gets the current project
    var project = Project.Current;
    //Get default gdb path
    var projGDBPath = Project.Current.DefaultGeodatabasePath;

From https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-content#get-the-projects-default-gdb-path

.NET ArcMap 10

    IWorkspaceName name = new WorkspaceNameClass();
    name.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
    name.PathName = @"E:\Projects\WorldSoil\ProjectData.gdb";
    IGxCatalogDefaultDatabase gdb = app as IGxCatalogDefaultDatabase;
    gdb.DefaultDatabaseName = name;

From http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IGxCatalogDefaultDatabase_Interface/0014000000qw000000/

VBA ArcMap 10

    Private Function MxDocument_OpenDocument() As Boolean
    Dim pWorkspaceName As IWorkspaceName
    Set pWorkspaceName = New WorkspaceName
    pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"
    pWorkspaceName.PathName = "C:\Scratch\MyDefaultGDB.gdb"
    Dim pGxCatalogDefaultDatabase As IGxCatalogDefaultDatabase
    Set pGxCatalogDefaultDatabase = Application
    pGxCatalogDefaultDatabase.DefaultDatabaseName = pWorkspaceName
    End Function

Placing this code into the Normal template section means that the code will be executed every time an MXD is opened.

From https://gis.stackexchange.com/a/192717/108

1
  • Some good diggin'! I like seeing how to achieve the same thing, setting the default database, across the different ArcGIS environments.
    – Hornbydd
    Commented Jan 11, 2019 at 9:43
0

How To: Set the default Home folder and geodatabase location for new map documents:

Instructions provided describe how to create and update a registry string value used to override the default location of the Home folder for new documents. This process involves editing the system registry.

and

Alternatively, a sample toolbox is available on arcgis.com containing a single script tool.

1
  • -1 because things have changed. It was the best answer available a few years ago as there were no arcpy options but that's changed and it's now off topic (non-code answer to a "looking for a code method" question) Commented Jan 10, 2019 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.