Managing Databases A database is a named collection of SQL objects (database objects); every database object (tables, function, etc.) belongs to one and only one database. An application that connects to the database server specifies with its connection request the name of the database it wants to connect to. It is not possible to access more than one database per connection. (But an application is not restricted in the number of connections it opens to the same or other databases.) SQL calls databases catalogs, but there is no difference in practice. In order to create or drop databases, the Postgres postmaster must be up and running (see ). Creating a Database Databases are created with the query language command CREATE DATABASE: CREATE DATABASE name where name can be chosen freely. (Depending on the current implementation, certain characters that are special to the underlying operating system might be prohibited. There will be run-time checks for that.) The current user automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later on (which also removes all the objects in it, even if they have a different owner). The creation of databases is a restricted operation. See how to grant permission. Bootstrapping Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See .) This database is called template1 and cannot be deleted. So to create the first real database you can connect to template1. The name template1 is no accident: When a new database is created, the template database is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. This implies that you should not use the template database for real work, but when used judiciously this feature can be convenient. As an extra convenience, there is also a program that you can execute from the shell to create new databases, createdb. createdb dbname createdb does no magic. It connects to the template1 database and executes the CREATE DATABASE command, exactly as described above. It uses psql program internally. The reference page on createdb contains the invocation details. In particular, createdb without any arguments will create a database with the current user name, which may or may not be what you want. Alternative Locations It is possible to create a database in a location other than the default. Remember that all database access occurs through the database server backend, so that any location specified must be accessible by the backend. Alternative database locations are referenced by an environment variable which gives the absolute path to the intended storage location. This environment variable must have been defined before the backend was started. Any valid environment variable name may be used to reference an alternative location, although using variable names with a prefix of PGDATA is recommended to avoid confusion and conflict with other variables. To create the variable in the environment of the server process you must first shut down the server, define the variable, initialize the data area, and finally restart the server. (See and .) To set an environment variable, type PGDATA2=/home/postgres/data export PGDATA2 in Bourne shells, or setenv PGDATA2 /home/postgres/data in csh or tcsh. You have to make sure that this environment variable is always defined in the server environment, otherwise you won't be able to access that database. Therefore you probably want to set it in some sort of shell start-up file or server start-up script. To create a data storage area in PGDATA2, ensure that /home/postgres already exists and is writable by the user account that runs the server (see ). Then from the command line, type initlocation PGDATA2 The you can restart the server. To create a database at the new location, use the command CREATE DATABASE name WITH LOCATION = 'location' where location is the environment variable you used, PGDATA2 in this example. The createdb command has the option Database created at alternative locations using this method can be accessed and dropped like any other database. It can also be possible to specify absolute paths directly to the CREATE DATABASE command without defining environment variables. This is disallowed by default because it is a security risk. To allow it, you must compile Postgres with the C preprocessor macro ALLOW_ABSOLUTE_DBPATHS defined. One way to do this is to run the compilation step like this: gmake CPPFLAGS=-DALLOW_ABSOLUTE_DBPATHS all. Accessing a Database Once you have constructed a database, you can access it by: running the Postgres terminal monitor program (psql) which allows you to interactively enter, edit, and execute SQL commands. writing a C program using the libpq subroutine library. This allows you to submit SQL commands from C and get answers and status messages back to your program. This interface is discussed further in the PostgreSQL Programmer's Guide. You might want to start up psql, to try out the examples in this manual. It can be activated for the dbname database by typing the command: psql dbname You will be greeted with the following message: Welcome to psql, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit dbname=> This prompt indicates that the terminal monitor is listening to you and that you can type SQL queries into a workspace maintained by the terminal monitor. The psql program responds to escape codes that begin with the backslash character, "\". For example, you can get help on the syntax of various Postgres SQL commands by typing: dbname=> \h Once you have finished entering your queries into the workspace, you can pass the contents of the workspace to the Postgres server by typing: dbname=> \g This tells the server to process the query. If you terminate your query with a semicolon, the backslash-g is not necessary. psql will automatically process semicolon terminated queries. To read queries from a file, instead of entering them interactively, type: dbname=> \i filename To get out of psql and return to Unix, type dbname=> \q and psql will quit and return you to your command shell. (For more escape codes, type backslash-h at the monitor prompt.) White space (i.e., spaces, tabs and newlines) may be used freely in SQL queries. Single-line comments are denoted by two dashes ("--"). Everything after the dashes up to the end of the line is ignored. Multiple-line comments, and comments within a line, are denoted by "/* ... */", a convention borrowed from Ingres. Destroying a Database Databases are destroyed with the command DROP DATABASE: DROP DATABASE name Only the owner of the database (i.e., the user that created it) can drop databases. Dropping a databases removes all objects that were contained within the database. The destruction of a database cannot be undone. You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database, which would be the only option for dropping the last database of a given cluster. For convenience, there is also a shell program to drop databases: dropdb dbname (Unlike createdb, it is not the default action to drop the database with the current user name.)