Create the database

The creation of a PostgreSQL database can be summarized in the following three steps: initializing the database storage area, starting the database server, and creating the database.

Initialize database storage area

The database storage area in PostgreSQL is called database cluster, which is used to store the files needed to run the database.

When the Data Save directory is specified during installation, PostgreSQL completes the initialization of the database storage area after installation. During the installation process, the initialization of the database storage area may fail, resulting in no content in the specified directory. In this case, you need to handle it manually, as shown below.

  1. Create a new Data Save directory "data". This directory is placed under the installation path of PostgreSQL (for example, C: \ Program Files \ PostgreSQL \ 9.0), and this directory cannot be the Data Save directory specified during installation.
  2. Use the command line to enter the "bin" directory under the PostgreSQL installation directory.

    cd "C:\Program Files\PostgreSQL\9.0\bin"

  3. To initialize the database, on the command line, enter:
    Copy
      initdb -D "C:\Program Files\PostgreSQL\9.0\data" --no-locale -U postgres  

    Explanation:

    • -D: Specifies the database storage path.
    • --no-locale: --no-locale = = -locale = C, that is, the runtime language Environment Settings is: No locale is used.
    • -U: The user specifies the name of the superuser, which was automatically created by the installer during the previous installation process-postgres.

Start the database server

  1. Create a new folder named "log" under the PostgreSQL installation directory, and create a new log file named "pgsql. Log" "under the folder.
  2. Command to start the database server.
    Copy
      pg_ctl -D "C:\Program Files\PostgreSQL\9.0\data" -l "C:\Program Files\PostgreSQL\9.0\log\pgsql.log" start  

    Explanation:

    • -D: Specifies the database storage path.
    • -l: Log path.
    • start/stop/restart/status.

Create the database

There are three ways to create a PostgreSQL database:

  • Start the database service using the client (pgAdminIII) provided by PostgreSQL, and create the database.
  • Use the SQL Shell provided by PostgreSQL to create a database using SQL statements.
  • Take advantage of PostgreSQL's createdb command.

No matter which method is used to create the database, after the database is created successfully, you can enter the database for various SQL operations.

  1. As shown in the following figure, right click the Database group of the started database server, and click the New Database.. "Menu to pop up the New Database" page window, and then a new database named SuperMap can be created. See the Modify Configuration file

    for how to configure the server in the pgAdminIII utility.

    Figure: New Database Operation
    Figure: New Database "Dialog Box
  2. SQL Shell Create Database

    Start the SQL Shell tool for PostgreSQL, as shown in the following figure. After successful login, you can type the SQL statement to create the database.

    Figure: SQL Shell Tool

    The SQL statement is as follows:

    Copy
      create database mydb;  
  3. Createdb command

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

    Copy
      create smdb;