PostgreSQL Managing Database


CREATE DATABASE creates a new PostgreSQL database. To create a database, you must be a superuser or have the special CREATEDB privilege. By default, the new database will be created by cloning the standard system database template1.

Database Options:

name:

    The name of a database to create.

user_name:

    The role name of the user who will own the new database, or DEFAULT to use the default (namely, the user executing the command). To create a database owned by another role, you must be a direct or indirect member of that role, or be a superuser.

template:

    The name of the template from which to create the new database, or DEFAULT to use the default template (template1).

encoding:

    Character set encoding to use in the new database. Specify a string constant (e.g., 'SQL_ASCII'), or an integer encoding number, or DEFAULT to use the default encoding (namely, the encoding of the template database). 

tablespace_name:

    The name of the tablespace that will be associated with the new database, or DEFAULT to use the template database's tablespace. This tablespace will be the default tablespace used for objects created in this database. See CREATE TABLESPACE for more information.

allowconn:

    If false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such as GRANT/REVOKE CONNECT).

connlimit:

    How many concurrent connections can be made to this database. -1 (the default) means no limit.

istemplate:

    If true, then this database can be cloned by any user with CREATEDB privileges; if false (the default), then only superusers or the owner of the database can clone it.

PostgreSQL Default tablespace:

  • Postgres
  • Template 1
  • Template 0

How to create database:

Postgresql database create for 2 ways.

Command mode:      

createdb     //utility tool
Usage:
  createdb [OPTION]... [DBNAME] [DESCRIPTION]
Options:
  -D, --tablespace=TABLESPACE  default tablespace for the database
  -e, --echo                   show the commands being sent to the server
  -E, --encoding=ENCODING      encoding for the database
  -l, --locale=LOCALE          locale settings for the database
      --lc-collate=LOCALE      LC_COLLATE setting for the database
      --lc-ctype=LOCALE        LC_CTYPE setting for the database
  -O, --owner=OWNER            database user to own the new database
  -T, --template=TEMPLATE      template database to copy
  -V, --version                output version information, then exit
  -?, --help                   show this help, then exit

Goto bin path:

-bash-4.1$ ./createdb mms -O muthu -T template0

Query mode:

postgres=# create database db;

postgres=# create database db1 allow_connections true;

postgres=# create database db2 connection limit 5;

postgres=# create database db2 owner muthu;

How to drop database?

-bash-4.1$ ./dropdb mms            //command mode

postgres=# drop database db;   //query mode

Comments

Popular posts from this blog

PostgreSQL pg_pool-II Installation and Configuration

PostgreSQL Migration Using MTK

PostgreSQL Pages and Tuples