Article
· May 18, 2023 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. *

* 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 *

Set Directory="/CacheDB/AAA/"
Set x=##class(%File).CreateDirectoryChain(Directory,.return)
If ('x) {
  Write "Error Creating Directory Chain: "_return
}
Else {
  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
Formatting...
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.

About ^DATABASE【IRIS】
About ^DATABASE

Discussion (3)3
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:

https://docs.intersystems.com/iris20231/csp/documatic/%25CSP.Documatic.c...

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

HTH!

Also you can create NAMESPACE/DATABASE via SQL. e.g.:

USER>:sql

SQL Command Line Shell

----------------------------------------------------


The command prefix is currently set to: <<nothing>>.

Enter <command>, 'q' to quit, '?' for help.

[SQL]USER>>Create Database TEST

1. Create Database TEST


0 Rows Affected

statement prepare time(s)/globals/cmds/disk: 0.0235s/2,162/14,045/0ms

          execute time(s)/globals/cmds/disk: 0.0843s/29,325/395,226/0ms

                          cached query class: %sqlcq.USER.cls21

---------------------------------------------------------------------------

[SQL]USER>>exit


USER>zn "TEST"


TEST>

HTH