【PostgreSQL】从零开始:(九)PostgreSQL-SQL语句操作数据库

news/2024/7/9 20:50:50 标签: 数据库, postgresql

数据库实例与数据库

Oracle中数据库实例与数据库是一对一的关系

PostgreSQL和MySQL中一个数据库实例对应多个数据库

数据库的相关操作

创建数据库

命令
CREATE DATABASE name
    [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ STRATEGY [=] strategy ] ]
           [ LOCALE [=] locale ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ ICU_LOCALE [=] icu_locale ]
           [ ICU_RULES [=] icu_rules ]
           [ LOCALE_PROVIDER [=] locale_provider ]
           [ COLLATION_VERSION = collation_version ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ]
           [ OID [=] oid ]
 参数
参数中文解释
OWNER给创建的数据库分配一个角色,这将是数据库的所有者。如果你省略了OWNER选项,数据库的所有者是执行CREATE DATABASE时的角色。
TEMPLATE默认情况下,PostgreSQL使用template指定从中创建新数据库的模板数据库。如果未明确指定模板数据库,则将默认数据库作为模板数据库.默认tempate1
ENCODING 确定新数据库中的字符集编码。默认语言与系统语言一致
LOCALE 设置新数据库中的默认排序规则顺序和字符分类。排序规则影响应用于字符串的排序顺序,例如,在使用order BY的查询中,以及在文本列的索引中使用的顺序。字符分类会影响字符的分类,例如低位、高位和数字。还设置操作系统环境的相关方面LC_COLLATE和LC_CTYPE。默认设置与模板数据库的设置相同。
LC_COLLATE指定排序规则顺序 (LC_COLLATE),新数据库将使用该排序规则。此参数影响的排序顺序字符串查询包含Order By模板数据库
LC_CTYPE指定新数据库将使用的字符分类。 它影响字符的分类,例如大写, 小写, 和数字. 它默认为模板数据库LC_CTYPE
TABLESPACE指定新数据库TABLESPACE的名称。默认值为模板数据库的表空间。默认表空间pg_default
CONNECTION LIMIT指定到新数据库的最大并发连接。默认值为-1,即无限制。此参数在共享托管环境中非常有用,我们可以在其中配置特定数据库的最大并发连接。
ALLOW_CONNECTIONS参数allow_connections的数据类型是布尔值。如果是false,我们无法连接到数据库
IS_TEMPLATE如果IS_TEMPLATE是真的,任何角色的CREATE DATABASE都可以克隆它。如果为false,则只有超级用户或数据库所有者可以克隆它。
ICU_Locale数据库的 ICU 排序规则,如果在初始化时指定了 ICU,则此字段会显示对应的规则。ICU 是一种提供强大本地化支持的库,包括复杂的排序规则和文本处理功能。
ICU_RULES指定附加排序规则以自定义此数据库的默认排序规则的行为。
LOCALE_PROVIDER指定用于此数据库中默认排序规则的提供程序。可能的值是 icu(如果服务器是使用 ICU 支持构建的)或 libc。默认情况下,提供者与模板的提供者相同。
COLLATION_VERSION指定与数据库一起存储的排序规则版本字符串。通常,应省略此项,这将导致根据操作系统提供的数据库排序规则的实际版本来计算版本。该选项旨在由 pg_upgrade 用于从现有安装复制版本。
例子
1.创建ci_database_test01
postgres=# create database ci_database_test01;
CREATE DATABASE
postgres=# \l
                                                            List of databases
        Name        |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |          |          |                 |             |             |            |           | postgres=CTc/postgres
(5 rows)

postgres=# 

创建的数据库ci_database_test01,所有者为postgres,字符集为UTF8,排序规则和语言符号及其分类为zh_CN.UTF-8

2.创建ci_database_test02,指定字符集为EUC_TW
postgres=# create database ci_database_test02 WITH ENCODING 'EUC_TW' LC_CTYPE="C" LC_COLLATE="C" TEMPLATE=template0;
CREATE DATABASE
postgres=# \l
                                                            List of databases
        Name        |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres | EUC_TW   | libc            | C           | C           |            |           | 
 circledba_test     | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |          |          |                 |             |             |            |           | postgres=CTc/postgres
(6 rows)

postgres=# 

衍生问题:

(1).为什么LC_CTYPE,LC_COLLATE设置为C?

答:设置为 C 是为了去除所有本地化的设置

(2).为什么一定要设置TEMPLATE=template0

答:使用 template1 模板库建库时不可指定新的 encoding 和 locale,而 template0 可以

(3).template1 和 template0 的区别?

答:1.template1 可以连接并创建对象,template0 不可以连接

postgres=# \c template1
You are now connected to database "template1" as user "postgres".
template1=# create table tmp_1( id int4);
CREATE TABLE
template1=# \c template0
FATAL: database "template0" is not currently accepting connections
Previous connection kept

template1 可以创建对像,被称为非干净数据库,而 template0被称为干净的数据库

(4).根据需要创建符合自己编码方式的数据库

再/etc/profile 文件里加入:

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

再执行

CREATE DATABASE ci_database_test03
WITH
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
template = template0
CONNECTION LIMIT = -1;
3.创建一个所有者(属主)为circledba用户的数据库ci_database_test04
postgres=# create user circledba with password '12345678ab';
CREATE ROLE
postgres=# create database ci_database_test03 WITH OWNER 'circledba';
CREATE DATABASE
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(7 rows)

postgres=# 
4.创建表空间为pg_circle的数据库ci_database_test05
mkdir -p /data/16.1/tablespace/dba_circle
chown -R postgres:postgres /data/16.1/tablespace/dba_circle
chmod -R 700 /data/16.1/tablespace/dba_circle
postgres=# create tablespace dba_circle location '/data/16.1/tablespace/dba_circle';
CREATE TABLESPACE
postgres=# create database ci_database_test05 WITH TABLESPACE 'dba_circle';
CREATE DATABASE
postgres=# \l+
                                                                                              List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   |  Size   | Tablespace |                Description                 
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------+---------+------------+--------------------------------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7561 kB | pg_default | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           |                       | 7481 kB | pg_default | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7561 kB | pg_default | 
 ci_database_test05 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7561 kB | dba_circle | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7724 kB | pg_default | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7636 kB | pg_default | default administrative connection database
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +| 7481 kB | pg_default | unmodifiable empty database
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres |         |            | 
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +| 7716 kB | pg_default | default template for new databases
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres |         |            | 
(8 rows)

postgres=# 
 

修改数据库

命令:
Command:     ALTER DATABASE
Description: change a database
Syntax:
ALTER DATABASE name [ [ WITH ] option [ ... ] ]

where option can be:

    ALLOW_CONNECTIONS allowconn
    CONNECTION LIMIT connlimit
    IS_TEMPLATE istemplate

ALTER DATABASE name RENAME TO new_name

ALTER DATABASE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }

ALTER DATABASE name SET TABLESPACE new_tablespace

ALTER DATABASE name REFRESH COLLATION VERSION

ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL

URL: https://www.postgresql.org/docs/16/sql-alterdatabase.html
例子
1.修改数据库名称
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test05 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test06 | circledba | EUC_TW   | libc            | C           | C           |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(9 rows)

postgres=# alter database ci_database_test06 rename to ci_database_test04;
ALTER DATABASE
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test04 | circledba | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test05 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(9 rows)

postgres=# 
2.修改数据库所属用户
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test04 | circledba | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test05 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(9 rows)

postgres=# alter database ci_database_test05 owner to circledba;
ALTER DATABASE
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test04 | circledba | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test05 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(9 rows)

postgres=# 

删除数据库

命令
DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]

where option can be:

    FORCE

URL: https://www.postgresql.org/docs/16/sql-dropdatabase.html
示例
1.删除ci_database_test05数据库
postgres=# DROP DATABASE ci_database_test05;
DROP DATABASE
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test04 | circledba | EUC_TW   | libc            | C           | C           |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(8 rows)

postgres=# 
2.强制删除ci_database_test04数据库

强制删除,意思是如果数据有进程在链接该数据普通删除是无法删掉的,需要加 FORCE命令

FORCE命令:如果当前用户无权终止其他连接,则此操作将会失败。所需权限与 pg_terminate_backend 相同

postgres=# DROP DATABASE ci_database_test04 WITH (FORCE);
DROP DATABASE
postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(7 rows)

postgres=# 

查看数据库

\l命令

postgres=# \l
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(7 rows)

postgres=#

\l+ 命令

postgres=# \l+
                                                                                              List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   |  Size   | Tablespace |                Description                 
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------+---------+------------+--------------------------------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7561 kB | pg_default | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           |                       | 7481 kB | pg_default | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7561 kB | pg_default | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7724 kB | pg_default | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |                       | 7636 kB | pg_default | default administrative connection database
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +| 7481 kB | pg_default | unmodifiable empty database
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres |         |            | 
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +| 7716 kB | pg_default | default template for new databases
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres |         |            | 
(7 rows)

postgres=# 

 \list命令

postgres=# \list
                                                            List of databases
        Name        |   Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
--------------------+-----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 ci_database_test01 | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 ci_database_test02 | postgres  | EUC_TW   | libc            | C           | C           |            |           | 
 ci_database_test03 | circledba | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 circledba_test     | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 postgres           | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | 
 template0          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
 template1          | postgres  | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
                    |           |          |                 |             |             |            |           | postgres=CTc/postgres
(7 rows)

postgres=# 

select 命令 

postgres=# SELECT datname FROM pg_database;
      datname       
--------------------
 postgres
 circledba_test
 template1
 template0
 ci_database_test01
 ci_database_test02
 ci_database_test03
(7 rows)

postgres=# 

PostgreSQL字符集参考表

NameDescriptionLanguageServer?ICU?Bytes/CharAliases
BIG5Big FiveTraditional ChineseNoNo1-2WIN950Windows950
EUC_CNExtended UNIX Code-CNSimplified ChineseYesYes1-3
EUC_JPExtended UNIX Code-JPJapaneseYesYes1-3
EUC_JIS_2004Extended UNIX Code-JP, JIS X 0213JapaneseYesNo1-3
EUC_KRExtended UNIX Code-KRKoreanYesYes1-3
EUC_TWExtended UNIX Code-TWTraditional Chinese, TaiwaneseYesYes1-3
GB18030National StandardChineseNoNo1-4
GBKExtended National StandardSimplified ChineseNoNo1-2WIN936Windows936
ISO_8859_5ISO 8859-5, ECMA 113Latin/CyrillicYesYes1
ISO_8859_6ISO 8859-6, ECMA 114Latin/ArabicYesYes1
ISO_8859_7ISO 8859-7, ECMA 118Latin/GreekYesYes1
ISO_8859_8ISO 8859-8, ECMA 121Latin/HebrewYesYes1
JOHABJOHABKorean (Hangul)NoNo1-3
KOI8RKOI8-RCyrillic (Russian)YesYes1KOI8
KOI8UKOI8-UCyrillic (Ukrainian)YesYes1
LATIN1ISO 8859-1, ECMA 94Western EuropeanYesYes1ISO88591
LATIN2ISO 8859-2, ECMA 94Central EuropeanYesYes1ISO88592
LATIN3ISO 8859-3, ECMA 94South EuropeanYesYes1ISO88593
LATIN4ISO 8859-4, ECMA 94North EuropeanYesYes1ISO88594
LATIN5ISO 8859-9, ECMA 128TurkishYesYes1ISO88599
LATIN6ISO 8859-10, ECMA 144NordicYesYes1ISO885910
LATIN7ISO 8859-13BalticYesYes1ISO885913
LATIN8ISO 8859-14CelticYesYes1ISO885914
LATIN9ISO 8859-15LATIN1 with Euro and accentsYesYes1ISO885915
LATIN10ISO 8859-16, ASRO SR 14111RomanianYesNo1ISO885916
MULE_INTERNALMule internal codeMultilingual EmacsYesNo1-4
SJISShift JISJapaneseNoNo1-2MskanjiShiftJISWIN932Windows932
SHIFT_JIS_2004Shift JIS, JIS X 0213JapaneseNoNo1-2
SQL_ASCIIunspecified (see text)anyYesNo1
UHCUnified Hangul CodeKoreanNoNo1-2WIN949Windows949
UTF8Unicode, 8-bitallYesYes1-4Unicode
WIN866Windows CP866CyrillicYesYes1ALT
WIN874Windows CP874ThaiYesNo1
WIN1250Windows CP1250Central EuropeanYesYes1
WIN1251Windows CP1251CyrillicYesYes1WIN
WIN1252Windows CP1252Western EuropeanYesYes1
WIN1253Windows CP1253GreekYesYes1
WIN1254Windows CP1254TurkishYesYes1
WIN1255Windows CP1255HebrewYesYes1
WIN1256Windows CP1256ArabicYesYes1
WIN1257Windows CP1257BalticYesYes1
WIN1258Windows CP1258VietnameseYesYes1ABCTCVNTCVN5712VSCII


http://www.niftyadmin.cn/n/5269035.html

相关文章

挑战52天学小猪佩奇笔记--day24

52天学完小猪佩奇--day24 ​【本文说明】 本文内容来源于对B站UP 脑洞部长 的系列视频 挑战52天背完小猪佩奇----day24 的视频内容总结,方便复习。强烈建议大家去关注一波UP,配合UP视频学习。 注:这集开始变成一段一段的猜台词,加…

文章解读与仿真程序复现思路——电网技术EI\CSCD\北大核心《耦合碳-绿证-消纳量市场的日前电量市场交易交互式优化》

这个标题描述了一种优化模型或算法,用于在日前电量市场中耦合碳排放权市场、可再生能源绿色证书市场和消纳量市场进行交易的交互式优化。我将解析标题的关键词和概念: 日前电量市场:指的是电力市场中进行短期调度和交易的市场,其…

LeetCode day25

LeetCode day25 额。今天做的题挺多是递推和dp,已经开始嘴角上扬了(嘛,大概也不用特别发出来,毕竟这会刷基础) 2278. 字母在字符串中的百分比 示例 1: 输入:s "foobar", letter "o" 输出&…

03-MySQL中的单行处理函数和多行处理函数

数据处理/单行处理函数 字段名既可以直接进行数字运算也可以被处理函数处理,如函数(字段名) ,即对该字段中所有的数据进行相应处理,处理结果可能是一个输出也可能是多个输出 单行处理函数:一个输入对应一个输出多行处理函数:多个输入对应一个输出 以下…

Redis设计与实现之对象处理机制

目录 一、前言 二、对象处理机制 1、redisObject 数据结构,以及 Redis 的数据类型 2、 命令的类型检查和多态 3、对象共享 4、引用计数以及对象的销毁 三、对象的处理 1、Redis是如何处理字符串对象的? 2、Redis是如何处理列表对象的&#xff1f…

【JAVA日志框架】JUL,JDK原生日志框架详解。

前言 Java日志体系混乱?Java日志框架系列,清晰简洁整理好整个Java的日志框架体系。第一篇,JDK原生日志框架——JUL。 目录 1.概述 2.日志级别 3.配置 4.继承关系 1.概述 日志框架的核心问题: 日志是用来记录应用的一些运行…

双色球c语言模拟双色球游戏的代码

双色球是一种流行的彩票游戏&#xff0c;其游戏规则是从红色球&#xff08;编号为1-33&#xff09;中随机选出6个&#xff0c;同时从蓝色球&#xff08;编号为1-16&#xff09;中随机选出1个。以下是一个简单的使用C语言模拟双色球游戏的代码&#xff1a; #include <stdio.…

[c++]—vector类___提升版(带你了解vector底层的运用)

我写我 不论主谓宾 可以反复错 &#x1f308;vector的介绍 1.vector是表示可变大小数组的序列容器2.就像数组一样&#xff0c;vector也采用的连续存储空间来存储元素&#xff0c;也就是意味着可以采用下标对vector的元素进行访问&#xff0c;和数组一样高效。但是又不像数组&…