获取 Postgres 数据库的编码

发布于 2024-11-16 23:23:53 字数 39 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

埋葬我深情 2024-11-23 23:23:53

从命令行:

psql my_database -c 'SHOW SERVER_ENCODING'

psql、SQL IDE 或 API 中:

SHOW SERVER_ENCODING;

From the command line:

psql my_database -c 'SHOW SERVER_ENCODING'

From within psql, an SQL IDE or an API:

SHOW SERVER_ENCODING;
旧竹 2024-11-23 23:23:53

方法 1:

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

SHOW SERVER_ENCODING;

结果:

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

对于客户端编码:

 SHOW CLIENT_ENCODING;

方法 2:

如果您已经登录,请再次使用此方法获取基于列表的结果

\l 

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 
请持续率性 2024-11-23 23:23:53

程序化解决方案:

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

A programmatic solution:

SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'yourdb';
初见你 2024-11-23 23:23:53

如果你想获取数据库编码:

psql  -U postgres -h somehost --list

你会看到类似的内容:

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

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
耶耶耶 2024-11-23 23:23:53

tl;dr

SELECT character_set_name 
FROM information_schema.character_sets 
;

标准方式:information_schema

来自 SQL 标准架构 <每个数据库/目录中都存在 href="https://www.postgresql.org/docs/current/static/information-schema.html" rel="noreferrer">information_schema ,使用名为 character_sets 的已定义视图。这种方法应该可以跨所有标准数据库系统移植。

SELECT * 
FROM information_schema.character_sets 
;

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

pgAdmin 的屏幕截图4 查询结果如上所示

第三列是character_set_name

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

tl;dr

SELECT character_set_name 
FROM information_schema.character_sets 
;

Standard way: information_schema

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

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

The third column is character_set_name:

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

压抑⊿情绪 2024-11-23 23:23:53

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

psql -l

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

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

psql -l

Shows all the database names, encoding, and more.

爱她像谁 2024-11-23 23:23:53

获取服务器编码的另一种方法(参见 https://pgpedia.info/s/server_encoding.html< /a>):

 SELECT current_setting('server_encoding');

还可以使用类似的选择来获取其他设置,例如“client_encoding”。

Another way of getting the server encoding (described at https://pgpedia.info/s/server_encoding.html):

 SELECT current_setting('server_encoding');

One can also use a similar select to get other settings, e.g. 'client_encoding'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文