从命令行执行查询时PostgreSQL编码问题 [英] PostgreSQL encoding issue while executing query from command line

查看:145
本文介绍了从命令行执行查询时PostgreSQL编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行存储在文件中的SQL查询。我正在使用以下命令执行:

I am trying to execute an SQL query which is stored in the file. I am using following command to execute:

psql -d DB_NAME -a -f QUERY_NAME.sql

我在SQL文件中有一些非英语文本,例如-સુરત

I have some non English text in the SQL file like - સુરત

执行查询时,数据库中的文本看起来像--«ÂÂÂÂÂÂÂÂÂ

When the query is executed the text in the database looks like - à ª¸à «Âà ª°à ª¤

如何从执行查询命令行,以便其正常运行?

How do I execute the query from command line so that it runs correctly?

推荐答案

确保 client_encoding 匹配文件的编码。检查您的系统区域设置。然后为psql使用匹配的命令行参数。 在此处引用该手册:

Make sure the client_encoding matches the encoding of your file. Check your system locale. Then use a matching command line argument for psql. Quoting the manual here:


如果标准输入或标准输出中至少有一个是终端,则
然后psql将客户端编码设置为自动,这将检测到
来自区域设置的适当客户端编码(Unix系统上的LC_CTYPE
环境变量)。如果这不能按预期的
进行,则可以使用环境
变量PGCLIENTENCODING覆盖客户端编码。

If at least one of standard input or standard output are a terminal, then psql sets the client encoding to "auto", which will detect the appropriate client encoding from the locale settings (LC_CTYPE environment variable on Unix systems). If this doesn't work out as expected, the client encoding can be overridden using the environment variable PGCLIENTENCODING.

Linux shell的示例:

Example for a Linux shell:

env PGCLIENTENCODING='WIN1258' psql DB_NAME -a -f QUERY_NAME.sql

手册中可用编码的列表。

这篇关于从命令行执行查询时PostgreSQL编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆