上一章《postgresql12在Linux上的 RPM 安装》介绍了如何linux环境安装postgresql12,接下来继续了解如何使用 postgresql12。
创建一个hr数据库
CREATE DATABASE HR;
当不清楚相关语法的时候,可以在命令行中执行\help 查看帮助
postgres=# \help create database
Command: CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ]
[ ALLOW_CONNECTIONS [=] allowconn ]
[ CONNECTION LIMIT [=] connlimit ]
[ IS_TEMPLATE [=] istemplate ] ]
URL: https://www.postgresql.org/docs/12/sql-createdatabase.html
CREATE DATABASE有几个选项
-
OWNER
数据库归属的用户,如果不指定,则取执行create database命令的用户。 -
TEMPLATE
创建数据库使用的模板,默认 template1 -
ENCODING
指定编码,如果不指定,则使用创建模板中的编码。比如,如果使用template1,则使用UTF8编码。
postgres=# \c template1
You are now connected to database "template1" as user "postgres".
template1=# \encoding
UTF8
- LC_COLLATE 、LC_CTYPE
排序规则,这两个参数影响数据排序。
template1=# \l template1
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
字符集支持的LC_COLLATE和LC_CTYPE信息可以查看pg_collation表。
- TABLESPACE
postgresql中的表空间跟oracle中不太一样。oracle中的表空间代表着一个容器,将数据对象放到这个容器中。而postgresql中的表空间则标识了一个数据挂载点。
但是两者都可以达到分散数据热点,优化磁盘分配的功能。
postgresql初始化后会创建两个表空间 pg_default、pg_global,如果建库没有指定表空间,则相关数据文件默认建立在pg_default下。
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+----------
pg_default | postgres |
pg_global | postgres |
(2 rows)
postgres=# CREATE TABLESPACE tb1 LOCATION '/data/tb1';
CREATE TABLESPACE
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+-----------
pg_default | postgres |
pg_global | postgres |
tb1 | postgres | /data/tb1
- ALLOW_CONNECTIONS
是否允许连接,默认是允许
postgres=# create database tb2 ALLOW_CONNECTIONS =false;
CREATE DATABASE
postgres=# \c tb2
FATAL: database "tb2" is not currently accepting connections
Previous connection kept
- CONNECTION LIMIT
连接数限制,默认为-1 ,意为 没有限制。
- IS_TEMPLATE
是否生成一个建库模板,默认为否
删除数据库的命令就很简单了,没有那么多选项
postgres=# \help drop database;
Command: DROP DATABASE
Description: remove a database
Syntax:
DROP DATABASE [ IF EXISTS ] name
URL: https://www.postgresql.org/docs/12/sql-dropdatabase.html
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




