php postgre添加数据库,PostgreSQL创建数据库

本章讨论在PostgreSQL中如何创建一个新的数据库, PostgreSQL提供两种方式创建一个新的数据库:

www.yiibai.com

使用CREATE DATABASE的SQL命令。

yiibai.com

使用createdb的一个命令行可执行文件。 www.yiibai.com

使用CREATE DATABASE

该命令将创建一个数据库PostgreSQL的shell提示符,但你应该有适当的权限来创建数据库。默认情况下,创建新的数据库将通过克隆标准系统数据库template1。

www.yiibai.com

语法:

CREATE DATABASE语句的基本语法如下: yiibai.com

CREATE DATABASE dbname; www.yiibai.com

其中dbname是要创建的数据库的名称。

www.yiibai.com

例子

下面是一个简单的例子,这将创建testdb 在PostgreSQL模式:  yiibai.com

postgres=#CREATE DATABASE testdb;postgres-#

yiibai.com

使用createdb的命令

PostgreSQL命令行可执行createdb是是SQL命令CREATE DATABASE一个包装器。此命令和SQL命令CREATE DATABASE之间唯一的区别是,前者可以直接在命令行中运行,它允许的注释被添加到数据库中,全部在一个命令。

www.yiibai.com

语法:

createdb语法如下所示: www.yiibai.com

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

yiibai.com

参数

下表列出了参数及它们的描述。

www.yiibai.com

参数名称

描述

dbname

The name of a database to create.

description

Specifies a comment to be associated with the newly created database.

options

command-line arguments which createdb accepts.

选项

下表列出了命令行参数CREATEDB接收:

yiibai.com

选项

描述

-D tablespace

Specifies the default tablespace for the database.

-e

Echo the commands that createdb generates and sends to the server.

-E encoding

Specifies the character encoding scheme to be used in this database.

-l locale

Specifies the locale to be used in this database.

-T template

Specifies the template database from which to build this database.

--help

Show help about dropdb command line arguments, and exit.

-h host

Specifies the host name of the machine on which the server is running.

-p port

Specifies the TCP port or the local Unix domain socket file extension on which the server is listening for connections.

-U username

User name to connect as.

-w

Never issue a password prompt.

-W

Force createdb to prompt for a password before connecting to a database.

打开命令提示符,然后去是PostgreSQL安装所在的目录。进入到bin目录,执行下面的命令创建一个数据库。

www.yiibai.com

createdb-h localhost-p5432-U postgress testdb

password******

www.yiibai.com

上面的命令会提示Postgres的默认的PostgreSQL管理用户的密码,以便提供密码和继续创建新的数据库。 yiibai.com

一旦创建数据库时可以使用上述方法,可以检查它在列表中的数据库使用\l即反斜线el命令如下: yiibai.com

postgres-#\lListof databasesName|Owner|Encoding|Collate|Ctype|Accessprivileges-----------+----------+----------+---------+-------+-----------------------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|(4rows)postgres-# www.yiibai.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值