postgresql 获取 Postgres 数据库的编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6454146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:59:11  来源:igfitidea点击:

Getting the encoding of a Postgres database

postgresql

提问by Elitmiar

I have a database, and I need to know the default encoding for the database. I want to get it from the command line.

我有一个数据库,我需要知道数据库的默认编码。我想从命令行获取它。

回答by Bohemian

From the command line:

从命令行:

psql my_database -c 'SHOW SERVER_ENCODING'


From within psql, an SQL IDE or an API:

从内部psql,SQL IDE 或 API:

SHOW SERVER_ENCODING

回答by RunningAdithya

Method 1:

方法一:

If you're already logged in to the db server, just copy and paste this.

如果您已经登录到数据库服务器,只需复制并粘贴它。

SHOW SERVER_ENCODING;

Result:

结果:

  server_encoding 
-----------------  
UTF8

For Client encoding :

对于客户端编码:

 SHOW CLIENT_ENCODING;

Method 2:

方法二:

Again if you are already logged in, use this to get the list based result

同样,如果您已经登录,请使用它来获取基于列表的结果

\l 

回答by Peter Eisentraut

A programmatic solution:

程序化解决方案:

SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'yourdb';

回答by Greenisha

If you want to get database encodings:

如果要获取数据库编码:

psql  -U postgres -h somehost --list

You'll see something like:

你会看到类似的东西:

List of databases
           Name         |  Owner   | Encoding
------------------------+----------+----------
db1                     | postgres | UTF8

回答by AMADANON Inc.

Because there's more than one way to skin a cat:

因为给猫剥皮的方法不止一种:

psql -l

Shows all the database names, encoding, and more.

显示所有数据库名称、编码等。

回答by Basil Bourque

tl;dr

tl;博士

SELECT character_set_name 
FROM information_schema.character_sets 
;

Standard way: information_schema

标准方式: information_schema

From the SQL-standard schemainformation_schemapresent in every database/catalog, use the defined view named character_sets. This approach should be portable across all standard database systems.

从每个数据库/目录中存在的SQL 标准模式information_schema中,使用名为character_sets. 这种方法应该可以跨所有标准数据库系统移植。

SELECT * 
FROM information_schema.character_sets 
;

Despite the name being plural, it shows only a single row, reporting on the current database/catalog.

尽管名称是复数,但它仅显示一行,报告当前数据库/目录。

screenshot of pgAdmin 4 with results of query shown above

pgAdmin 4 的屏幕截图,上面显示了查询结果

The third column is character_set_name:

第三列是character_set_name

Name of the character set, currently implemented as showing the name of the database encoding

字符集名称,目前实现为显示数据库编码的名称