Monday, February 27, 2017

PostgreSQL Database Manipulation

- Create Database


1. Using sql command CREATE DATABASE

By default, the new database will be created by cloning the standard system database template1.

Syntax

The basic syntax of CREATE DATABASE statement is as follows:
CREATE DATABASE dbname;
where dbname is the name of a database to create.

Example

Following is a simple example, which will create testdb in your PostgreSQL schema:
postgres=# CREATE DATABASE testdb;
postgres-# 

2. Using createdb Command

PostgreSQL command line executable createdb is a wrapper around the SQL command CREATE DATABASE

Syntax

The syntax for createdb is as shown below:
createdb [option...] [dbname [description]]

Parameters

Table below lists the parameters with their descriptions.
ParameterDescription
dbnameThe name of a database to create.
descriptionSpecifies a comment to be associated with the newly created database.
optionscommand-line arguments, which createdb accepts.

Options

The following table lists the command line arguments createdb accepts:
OptionDescription
-D tablespaceSpecifies the default tablespace for the database.
-eEcho the commands that createdb generates and sends to the server.
-E encodingSpecifies the character encoding scheme to be used in this database.
-l localeSpecifies the locale to be used in this database.
-T templateSpecifies the template database from which to build this database.
--helpShow help about createdb command line arguments, and exit.
-h hostSpecifies the host name of the machine on which the server is running.
-p portSpecifies the TCP port or the local Unix domain socket file extension on which the server is listening for connections.
-U usernameUser name to connect as.
-wNever issue a password prompt.
-WForce createdb to prompt for a password before connecting to a database.
Open the command prompt and go to the directory where PostgreSQL is installed. Go to the bin directory and execute the following command to create a database.
createdb -h localhost -p 5432 -U postgres testdb
password ******
Above command will prompt you for password of the PostgreSQL admin user which is postgres by default so provide password and proceed to create your new dataabse.
Once a database is created using either of the above-mentioned methods, you can check it in the list of databases using \l, i.e., backslash el command as follows:
postgres-# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C       | C     | 
(4 rows)

postgres-# 

No comments:

Post a Comment

NYSE:ORCL -- Oracle

26-03-11 ☁️ Oracle:杠杆的艺术 2026财年第三季度关键指标: ☁️营收增长加速(但存在一些问题):总营收达到 172 亿美元,同比增长 22%。按固定汇率计算,增长率仅为 18%,这意味着部分增长来自汇率波动,而非实际需求。 📦 积压订单持续攀升:RPO 达...