PostgreSQL Database

PostgreSQL Database

Using CREATE DATABASE

Syntax

CREATE DATABASE dbname;

Using createdb Command

Syntax

createdb [option...] [dbname [description]]

Parameters

S.No. Parameter & Description
1 dbname The name of a database to create.
2 description Specifies a comment to be associated with the newly created database.
3 options command-line arguments, which createdb accepts.

Options

S.No. Parameter & Description
1 -D tablespace Specifies the default tablespace for the database.
2 -e Echo the commands that createdb generates and sends to the server.
3 -E encoding Specifies the character encoding scheme to be used in this database.
4 -l locale Specifies the locale to be used in this database.
5 -T template Specifies the template database from which to build this database.
6 --help Show help about createdb command line arguments, and exit.
7 -h host Specifies the host name of the machine on which the server is running.
8 -p port Specifies the TCP port or the local Unix domain socket file extension on which the server is listening for connections.
9 -U username User name to connect as.
10 -w Never issue a password prompt.
11 -W Force createdb to prompt for a password before connecting to a database.

Example

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 ******

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-# 

Database SQL Prompt

Type the following command to connect/select a desired database; here, we will connect to the testdb database.

postgres=# \c testdb;
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=# 

OS Command Prompt

You can select your database from the command prompt itself at the time when you login to your database. Following is a simple example ?

psql -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=# 

You are now logged into PostgreSQL testdb and ready to execute your commands inside testdb. To exit from the database, you can use the command \q.

Using DROP DATABASE

Syntax

DROP DATABASE [ IF EXISTS ] name

Parameters

S.No. Parameter & Description
1 IF EXISTS Do not throw an error if the database does not exist. A notice is issued in this case.
2 name The name of the database to remove.

Using dropdb Command

Syntax

dropdb [option...] dbname

Parameter

S.No. Parameter & Description
1 dbname The name of a database to be deleted.
2 option command-line arguments, which dropdb accepts.

Options

The following table lists the command-line arguments dropdb accepts ?

S.No. Parameter & Description
1 -e Shows the commands being sent to the server.
2 -i Issues a verification prompt before doing anything destructive.
3 -V Print the dropdb version and exit.
4 --if-exists Do not throw an error if the database does not exist. A notice is issued in this case.
5 --help Show help about dropdb command-line arguments, and exit.
6 -h host Specifies the host name of the machine on which the server is running.
7 -p port Specifies the TCP port or the local UNIX domain socket file extension on which the server is listening for connections.
8 -U username User name to connect as.
9 -w Never issue a password prompt.
10 -W Force dropdb to prompt for a password before connecting to a database.
11 --maintenance-db=dbname Specifies the name of the database to connect to in order to drop the target database.

Example

dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末诽凌,一起剝皮案震驚了整個濱河市扎筒,隨后出現(xiàn)的幾起案子芳誓,更是在濱河造成了極大的恐慌,老刑警劉巖芥玉,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異备图,居然都是意外死亡灿巧,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門揽涮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抠藕,“玉大人,你說我怎么就攤上這事蒋困《芩疲” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵雪标,是天一觀的道長零院。 經(jīng)常有香客問我溉跃,道長,這世上最難降的妖魔是什么告抄? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任撰茎,我火速辦了婚禮,結(jié)果婚禮上玄妈,老公的妹妹穿的比我還像新娘乾吻。我一直安慰自己,他們只是感情好拟蜻,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布绎签。 她就那樣靜靜地躺著,像睡著了一般酝锅。 火紅的嫁衣襯著肌膚如雪诡必。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天搔扁,我揣著相機與錄音爸舒,去河邊找鬼。 笑死稿蹲,一個胖子當著我的面吹牛扭勉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苛聘,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涂炎,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了设哗?” 一聲冷哼從身側(cè)響起唱捣,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎网梢,沒想到半個月后震缭,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡战虏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年拣宰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烦感。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡徐裸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出啸盏,到底是詐尸還是另有隱情重贺,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站气笙,受9級特大地震影響次企,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜潜圃,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一缸棵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谭期,春花似錦堵第、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至胀瞪,卻和暖如春针余,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背凄诞。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工圆雁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人帆谍。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓伪朽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親汛蝙。 傳聞我的和親對象是個殘疾皇子烈涮,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 1 根據(jù)函數(shù)名字符串,調(diào)用函數(shù) getattr 參考: 通過函數(shù)名的字符串來調(diào)用這個函數(shù) 根據(jù)字符串名稱動態(tài)調(diào)用P...
    Tim_Lee閱讀 84評論 0 0
  • 它來自昨日患雇,灰藍色的世界,充滿褶皺宇挫。 褶皺是生命中的波折苛吱,起伏的熱情與褪色無關(guān)。 它來自夏天器瘪,此刻在冬眠翠储。 紅色使...
    劉君策閱讀 172評論 0 0