Postgresql学习笔记之——使用createdb来创建数据库

Postgresql数据库提供一种在shell下创建数据库的工具:createdb
createdb不是特殊的工具,可以说它是数据库创建命令的整合工具。它连接到postgres数据库并发出CREATE database命令,与在进入Postgresql数据库中执行 CREATE DATABASE 是一样的。可以看作是脚本命令。

一、createdb语法:

createdb [connection-option…] [option…] [dbname [description]]

语法解析(createdb命令接受一下参数来执行创建数据库时指定的各个参数):

1.dbname:执行新数据库的名称,不与现有数据库名称冲突,默认设置是创建与当前系统用户同名的数据库。

2.description:指定要与新创建的数据库关联的注释。

3.-D tablespace 或 --tablespace=tablespace:指定新数据库的默认表空间(注意表空间名称要加上双引号)

4.-e 或 --echo:显示 createdb 生成的并发送给数据库服务的相关命令详情。

5.-E encoding 或 --encoding=encoding:执行新数据库的字符集编码(字符集名称加双引号)。

6.-l locale 或 --locale=locale:指定此数据库中使用的语言环境。这相当于同时指定——lc-collate和——lc-ctype。

7.–lc-collate=locale:指定要在此数据库中使用的LC_COLLATE设置。

8.–lc-ctype=locale:指定要在此数据库中使用的LC_CTYPE设置。

9.-O owner 或 --owner=owner:指定将拥有新数据库的数据库用户。(此名称作为双引号标识符处理。)

10.-T template 或 --template=template:指定要从中构建此数据库的模板数据库。(此名称作为双引号标识符处理。)

11.-V 或 --version:打印 createdb 工具的版本。

12.-? 或 --help:帮助命令。

以上时createdb 工具创建数据库时所支持的参数指定,另外它还支持链接参数:

-h host
–host=host
Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix domain socket.

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

-U username
–username=username
User name to connect as.

-w
–no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

-W
–password
Force createdb to prompt for a password before connecting to a database.

This option is never essential, since createdb will automatically prompt for a password if the server demands password authentication. However, createdb will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.

–maintenance-db=dbname
Specifies the name of the database to connect to when creating the new database. If not specified, the postgres database will be used; if that does not exist (or if it is the name of the new database being created), template1 will be used.

二、环境变量的设置

1.PGDATABASE:If set, the name of the database to create, unless overridden on the command line.

2.PGHOST

3.PGPORT

4.PGUSER:Default connection parameters. PGUSER also determines the name of the database to create, if it is not specified on the command line or by PGDATABASE.

5.PG_COLOR:Specifies whether to use color in diagnostics messages. Possible values are always, auto, never.

四、示例

To create the database demo using the default database server:

$ createdb demo

To create the database demo using the server on host eden, port 5000, using the template0 template database, here is the command-line command and the underlying SQL command:

$ createdb -p 5000 -h eden -T template0 -e demo

CREATE DATABASE demo TEMPLATE template0;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以和你分享一些关于 PostgreSQL学习笔记。 1. PostgreSQL 是什么? PostgreSQL 是一种开源的关系型数据库管理系统,它支持 SQL 语言和 ACID 事务处理,并且具有丰富的特性和扩展性,可用于大型企业应用、Web 应用和嵌入式应用。 2. PostgreSQL 的特点 PostgreSQL 具有以下特点: - 开源免费 - 支持 ACID 事务处理 - 支持 SQL 语言 - 支持复杂查询和数据类型 - 支持存储过程和触发器 - 可以扩展和定制 - 可以跨平台运行 3. PostgreSQL 的安装 在 Windows 平台下,可以从官网下载 Windows 安装程序进行安装。在 Linux 平台下,可以使用包管理器安装,例如 Ubuntu 可以使用以下命令进行安装: ``` sudo apt-get install postgresql ``` 4. PostgreSQL 的基本操作 创建数据库: ``` CREATE DATABASE databasename; ``` 创建表: ``` CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, .... ); ``` 插入数据: ``` INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 查询数据: ``` SELECT column1, column2, ... FROM tablename WHERE condition; ``` 更新数据: ``` UPDATE tablename SET column1 = value1, column2 = value2, ... WHERE condition; ``` 删除数据: ``` DELETE FROM tablename WHERE condition; ``` 5. PostgreSQL 的扩展 PostgreSQL 可以通过扩展来增强其功能,例如: - PostGIS:空间数据处理扩展 - PL/Python:Python 语言扩展 - pgAdmin:可视化管理工具 - pgpool-II:连接池和负载均衡工具 以上就是一些关于 PostgreSQL 的基本学习笔记,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Major_ZYH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值