Creating a Database

The creation of PostgreSQL database including the following steps: Initialize database cluster, start database server and create database.

Initialize database cluster

The data storage area in PostgreSQL is called database cluster, it is used to store the file needed for run the database.

If you specified data directory in the installation process, PostgreSQL will finish the initializing of the data directory when the installation finished. For the case that the database cluster initialization failed when installing, it may lead to the specified directory doesn’t contain any context and you need to process it manually, as shown below.

  1. Create data storage directory data , it is in the installation directory of PostgreSQL (such as: C:\Program Files\PostgreSQL\9.0), the directory can not be the data storage directory when installing.
  2. Enter the bin directory in PostgreSQL installation directory with command line.

cd “C:\Program Files\PostgreSQL\9.0\bin”

  1. Initialize database, input:
initdb -D "C:\Program Files\PostgreSQL\9.0\data" --no-locale -U postgres

Explaination:

  • -D : Specify the store directory of the database.
  • --no-locale : —no-locale==-locale=C, set the run time language environment as : no-locale.
  • -U : The name of the superuser specified, the installation application create the superuser automatically in the installing process----postgres.

Start Database Server

  1. Create a new folder named log in the PostgreSQL installation directory, then create a log file named pgsql.log in the folder.
  2. Start database server by command
pg_ctl -D "C:\Program Files\PostgreSQL\9.0\data" -| "C:\Program Files\PostgreSQL\9.0\log\pgsql.log" start

Explaination:

  • -D : Specify the store directory of the database.
  • -| :log directory.
  • start/stop/restart/status.

Create database

There are three methods to create PostgreSQL database:

  • Start the database service with the client provided by PostgreSQL (pgAdminIII) and create database.
  • Use the SQL Shell provided by PostgreSQL and create database with SQL.
  • Use the createdb command of PostgreSQL.

No matter which method you use, you can enter the database to perform all kinds of SQL operations after the database created successfully.

  1. As shown below, right click on the Databases in the Servers, and click New Database…, the New Database… dialog box appears, then create a database named SuperMap.

For more information about how to configure server in pgAdminIII, please refer to Modify Configuration File.

creatDatabase1

creatDatabase2

  1. SQL Shell create database

Start the SQL Shell tool of PostgreSQL, as shown below. Input the SQL statement after sign in successfully.

SQLShell

The SQL statement is as the following:

    	create database mydb;
  1. createdb command

Enter the bin directory in PostgreSQL installation directory and use the createdb command.

    	create smdb;