jmarsac a écrit :Il faut vérifier/corriger l'encodage du fichier .txt contenant le script (D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt par exemple)
OK mais comment fait-on avec Windows ?
Le plus simple est d'utiliser notepad++ qui permet de contrôler et convertir l'encodage du fichier (client psql 12, server 11.2); j'ai utilisé deux scripts différents :
itest-ansi.sql (encodé en ANSI) :
\encoding
set client_encoding to 'Win1252';
show client_encoding;
\l b1
insert into t2 values ('öéùàè','ANSI');
select * from t2;
set client_encoding to 'UTF8';
show client_encoding;
select * from t2;
set client_encoding to 'Win1252';
show client_encoding;
select * from t2;
itest-utf8.sql (encodé en UTF8) :
\encoding
set client_encoding to 'Win1252';
show client_encoding;
\l b1
insert into t2 values ('öéùàè','UTF8');
select * from t2;
set client_encoding to 'UTF8';
show client_encoding;
select * from t2;
set client_encoding to 'Win1252';
show client_encoding;
select * from t2;
********* QUERY **********
create table t2 (a text,script_encoding text);
**************************
CREATE TABLE
********* QUERY **********
set client_encoding to 'Win1252';
**************************
SET
********* QUERY **********
show client_encoding;
**************************
client_encoding
-----------------
WIN1252
(1 row)
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------+----------+----------+--------------------+--------------------+-------------------
b1 | postgres | UTF8 | French_France.1252 | French_France.1252 |
(1 row)
********* QUERY **********
insert into t2 values ('öéùàè','ANSI');
**************************
INSERT 0 1
********* QUERY **********
select * from t2;
**************************
a | script_encoding
-------+-----------------
öéùàè | ANSI
(1 row)
********* QUERY **********
set client_encoding to 'UTF8';
**************************
SET
********* QUERY **********
show client_encoding;
**************************
client_encoding
-----------------
UTF8
(1 row)
********* QUERY **********
select * from t2;
**************************
a | script_encoding
-------+-----------------
öéùà è | ANSI
(1 row)
********* QUERY **********
set client_encoding to 'win1252';
**************************
SET
********* QUERY **********
select * from t2;
**************************
a | script_encoding
-------+-----------------
öéùàè | ANSI
(1 row)
********* QUERY **********
set client_encoding to 'Win1252';
**************************
SET
********* QUERY **********
show client_encoding;
**************************
client_encoding
-----------------
WIN1252
(1 row)
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------+----------+----------+--------------------+--------------------+-------------------
b1 | postgres | UTF8 | French_France.1252 | French_France.1252 |
(1 row)
********* QUERY **********
insert into t2 values ('öéùà è','UTF8');
**************************
INSERT 0 1
********* QUERY **********
select * from t2;
**************************
a | script_encoding
------------+-----------------
öéùàè | ANSI
öéùà è | UTF8
(2 rows)
********* QUERY **********
set client_encoding to 'UTF8';
**************************
SET
********* QUERY **********
show client_encoding;
**************************
client_encoding
-----------------
UTF8
(1 row)
********* QUERY **********
select * from t2;
**************************
a | script_encoding
------------+-----------------
öéùà è | ANSI
öéùàè | UTF8
(2 rows)
********* QUERY **********
set client_encoding to 'Win1252';
**************************
SET
********* QUERY **********
show client_encoding;
**************************
client_encoding
-----------------
WIN1252
(1 row)
********* QUERY **********
select * from t2;
**************************
a | script_encoding
------------+-----------------
öéùàè | ANSI
öéùà è | UTF8
(2 rows)
Comme le dit Pifor, l'important est de bien configurer l'encodage du client et d'utiliser la bonne page de code dans la console (chcp)
]]>Mon script en entrée:
select version();
\encoding
show client_encoding;
\l demo
\d t
truncate t;
insert into t values('Côte');
select * from t;
Résultat
select version();
version
-------------------------------------------------------------
PostgreSQL 10.12, compiled by Visual C++ build 1800, 64-bit
(1 ligne)
WIN1252
show client_encoding;
client_encoding
-----------------
WIN1252
(1 ligne)
Liste des bases de données
Nom | Propriétaire | Encodage | Collationnement | Type caract. | Droits d'accès
------+--------------+----------+--------------------+--------------------+----------------
demo | postgres | UTF8 | French_France.1252 | French_France.1252 |
(1 ligne)
Table « public.t »
Colonne | Type | Collationnement | NULL-able | Par défaut
---------+------+-----------------+-----------+------------
c | text | | |
truncate t;
TRUNCATE TABLE
insert into t values('Côte');
INSERT 0 1
select * from t;
c
------
Côte
(1 ligne)
postgres=# create database demo;
CREATE DATABASE
postgres=# \l demo;
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------+----------+----------+-------------+-------------+-------------------
demo | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(1 row)
postgres=#
Je constate que le paramètres LC_COLLATE et LC_CTYPE sont différents avec Windows: ce qui peut entraîner des comportements différents, par exemple dans le tri de chaînes de caractères.
]]>WIN1252
client_encoding
-----------------
WIN1252
(1 ligne)
Liste des bases de données
Nom | Propriétaire | Encodage | Collationnement | Type caract. | Droits d'accès
------------+--------------+----------+--------------------+--------------------+-------------------------------
intensetbm | postgres | UTF8 | French_France.1252 | French_France.1252 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | intensetbm_admin=CTc/postgres
(1 ligne)
INSERT 0 1
deleted | pay_ide | pay_nom_eng | pay_abr | pay_nom_fra
---------+---------+-------------+---------+----------------
| 2 | Ivory Coast | CIV | Côte d Ivoire
| 8 | Ivory Coast | CIV | Côte d Ivoire
| 9 | Ivory Coast | CIV | Côte d Ivoire
(3 lignes)
il ne faut considérer que la dernière ligne de la tabel
]]>LECARROU a écrit :j'ai modifier runpsql.bat et je n'ai plus l'avertissement au lancement de psql (cool)
si je fait
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
directement dans la console pslq pas de problème, il m'insère correctement les accents
mais si je lance le script depuis la console : \i 'D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt' toujours un problème sur les accents...
Il faut vérifier/corriger l'encodage du fichier .txt contenant le script (D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt par exemple)
OK mais comment fait-on avec Windows ?
]]>\encoding
show client_encoding;
\l intensetbm
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
select * from_adm_pay where pay_abr='CIV';
je me suis connecté via SQL Shell et j'obtiens le message suivant :
Server [localhost]:
Database [postgres]:
Port [5433]:
Username [postgres]: postgres
Page de codes active : 1252
Mot de passe pour l'utilisateur postgres :
psql (10.10)
Saisissez « help » pour l'aide.
postgres=#
à partir de là, je lance donc le script que vous m'avez donné (aucun SET de l'encoding):
\encoding
show client_encoding
\l intensetbm
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
select * from adm_pay where pay_abr='CIV';
et j'obtiens la sortie suivante :
WIN1252
Liste des bases de données
Nom | Propriétaire | Encodage | Collationnement | Type caract. | Droits d'accès
------------+--------------+----------+--------------------+--------------------+-------------------------------
intensetbm | postgres | UTF8 | French_France.1252 | French_France.1252 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | intensetbm_admin=CTc/postgres
(1 ligne)
les instructions SQL sortent en erreur
psql:D:/Users/jl3/intensetbm_test/intensetbm-etool/test.txt:4: ERREUR: erreur de syntaxe sur ou près de « INSERT »
LIGNE 2 : INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES...
^
psql:D:/Users/jl3/intensetbm_test/intensetbm-etool/test.txt:5: ERREUR: la relation « adm_pay » n'existe pas
LIGNE 1 : select * from adm_pay where pay_abr='CIV';
j'ai modifier runpsql.bat et je n'ai plus l'avertissement au lancement de psql (cool)
si je fait
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
directement dans la console pslq pas de problème, il m'insère correctement les accents
mais si je lance le script depuis la console : \i 'D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt' toujours un problème sur les accents...
Il faut vérifier/corriger l'encodage du fichier .txt contenant le script (D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt par exemple)
]]>\encoding
show client_encoding
\l intensetbm
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
select * from_adm_pay where pay_abr='CIV';
si je fait
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
directement dans la console pslq pas de problème, il m'insère correctement les accents
mais si je lance le script depuis la console : \i 'D:/Users/jl3/intensetbm_test/intensetbm-etool/script2.txt' toujours un problème sur les accents...
script1.txt
CREATE DATABASE intensetbm;
CREATE USER intensetbm_admin WITH PASSWORD 'admin';
ALTER ROLE intensetbm_admin SET client_encoding TO 'utf8';
ALTER ROLE intensetbm_admin SET default_transaction_isolation TO 'read committed';
ALTER ROLE intensetbm_admin SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE intensetbm TO intensetbm_admin;
script2.txt
SET client_encoding TO 'utf8';
INSERT INTO adm_pay (pay_nom_eng,pay_nom_fra,pay_abr) VALUES ('Ivory Coast','Côte d Ivoire','CIV');
je vais craquer... ; )
]]>