PostgreSQL9.1を利用し、あるオープンソースソリューションのバックエンドとして利用したとき、以下のエラーが発生した。
SELECT xxxxx WHERE xxxxx."xxxxx" = ? AND (xxxxx LIKE ? ESCAPE '\\' ) ORDER BY xxxxx ERROR: invalid escape string HINT: Escape string must be empty or one character..)
postgresql.confの設定を以下の値にして回避することできる。
standard_conforming_strings = off
PostgreSQL9.1から、postgresql.confファイルの standard_conforming_strings の値のデフォルト値がonに変更されたため。
以下にstandard_conforming_stringsがonの時とoffの時の動作例を2つ記述する。
$ psql template1
psql (9.1.0)
Type "help" for help.
template1=# show standard_conforming_strings;
standard_conforming_strings
-----------------------------
off
(1 row)
template1=# select 'abc\\\\';
WARNING: nonstandard use of \\ in a string literal
LINE 1: select 'abc\\\\';
^
HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
?column?
----------
abc\\
(1 row)
上記の実行例では、abc\\\\をselectで表示している。
エスケープされるので、実際はabc\\になる。
standard_conforming_strings = offなので、そのままの表示であるabc\\が表示された。
template1=# set standard_conforming_strings=on; SET template1=# show standard_conforming_strings; standard_conforming_strings ----------------------------- on (1 row) template1=# select 'abc\\\\'; ?column? ---------- abc\\\\ (1 row)
上記の実行例では、abc\\\\をselectで表示している。
エスケープされるので、実際はabc\\になる。
standard_conforming_strings = onなので、エスケープ文字である\を付加したabc\\\\が表示された。
以下は改行である\nを利用してテストした例。
こちらの方がわかりやすいかも。
template1=# set standard_conforming_strings=on;
SET
template1=# select 'hello\nworld\n';
?column?
----------------
hello\nworld\n
(1 row)
template1=# set standard_conforming_strings=off;
SET
template1=# select 'hello\nworld\n';
WARNING: nonstandard use of escape in a string literal
LINE 1: select 'hello\nworld\n';
^
HINT: Use the escape string syntax for escapes, e.g., E'\r\n'.
?column?
----------
hello +
world +
(1 row)
以下のメッセージは、postgresql.confのescape_string_warningをoffにすると出力されなくなる。
HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
postgrsql.conf内のescape_string_warningをoffに設定すること
escape_string_warning = off