· May 18 2m read

Command line interface and API to create namespaces and databases

InterSystems FAQ rubric

Using the Config.Configuration class and SYS.Database class methods, you can create and register a namespace database from the terminal.

Below is a series of execution examples that create database file /CacheDB/AAA/cache.dat and register database AAA and namespace AAA in the configuration file (cache.cpf).
* Execute in the %SYS namespace. *

Set Directory="/CacheDB/AAA/"
Set x=$ZF(-100, "/shell", "mkdir", Directory)
Set db=##Class(SYS.Database).%New()
Set db.Directory=Directory
Set status=db.%Save()
Set DBName="AAA"
Set status=##class(Config.Configuration).AddDatabase(DBName,Directory)
Set NSName=DBName
Set status=##class(Config.Configuration).AddNamespace(NSName,DBName)

To create a namespace from a remote database, specify the remote server name as the third parameter in the database definition below.

Set status=##class(Config.Configuration).AddDatabase(DBName,Directory,Server)

*For details on each class, please refer to each class reference.

You can also use the ^DATABASE routine for database creation and registration.

When you run this routine, the following options are displayed, so select them according to your purpose.

The following is an execution example of database creation.
* Execute in the %SYS namespace. *

%SYS>do ^DATABASE 1) Create a database
2) Edit a database
3) List databases
4) Delete a database
5) Mount a database
6) Dismount a database
7) Compact a database
8) Show free space for a database
9) Show details for a database
10) Recreate a database
11) Manage database encryption
Option? 1
Database directory? D:\200820DS\Mgr\TEST2
Directory does not exist, create it? No => y
Change default database properties? No =>
Dataset name of this database in the configuration: TEST2
Mount TEST2 Required At Startup? No => y
Confirm creation of database in d:\200820ds\mgr\test2\? Yes => y
Database in d:\200820ds\mgr\test2\ created
Dataset TEST2 added to the current configuration.
Database directory? 

*For each option of the ^DATABASE routine, please refer to the document page below.


Discussion (1)2
Log in or sign up to continue

@Hiroshi Sato - thank you for sharing!

I would recommend that rather than using $zf(-100) you use the %File class to make your directory creation OS-independent.  This will allow it to be used on different OSes.

Check out the CreateDirectoryChain() method of the %File class:

So your line "Set x=$ZF(-100, "/shell", "mkdir", Directory)" could be replaced by:

Set x=##class(%File).CreateDirectoryChain(Directory,.return)
If ('x) {
  Write "Error Creating Directory Chain: "_return

Also, make sure that this script runs as the user that is used for all IRIS processes to ensure that the directory has appropriate ownership and permissions