我尝试使用 Encoding: UTF8 创建一个带有表的数据库,并插入 COPY 命令尝试插入的两个 UTF-8 编码字符,并且在使用 INSERT 时它可以工作。
CREATE DATABASE test
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'English_United States.1252'
LC_CTYPE = 'English_United States.1252'
CONNECTION LIMIT = -1;
CREATE TABLE x
(
first_two_letters character(3)
)
WITH (
OIDS=FALSE
);
ALTER TABLE x
OWNER TO postgres;
INSERT INTO x(
first_two_letters)
VALUES ('سر');
根据http://rishida.net/tools/conversion/ http://rishida.net/tools/conversion/对于失败的 COPY,Unicode 代码点为:
U+0633 U+0631
哪个是两个字符,这意味着您应该能够将它们存储在定义为 character(3) 的列中,该列存储长度最多为 3 个字符(不是字节)的字符串。
如果我们尝试 INSERT,它会成功:
INSERT INTO x(
first_two_letters)
VALUES (U&'\0633\0631');
来自pg_dump 文档 http://www.postgresql.org/docs/9.3/static/app-pgdump.html您可以使用 --inserts 选项插入而不是复制
--插入
将数据转储为 INSERT 命令(而不是 COPY)。这会让恢复变得非常缓慢;它主要用于制作可以的转储
被加载到非 PostgreSQL 数据库中。然而,由于这个选项
为每一行生成一个单独的命令,重新加载行时出错
仅导致该行丢失,而不是整个表内容丢失。
请注意,如果您重新安排,恢复可能会完全失败
列顺序。 --column-inserts 选项对于列顺序是安全的
变化,尽管速度更慢。
尝试使用它来代替第 1 步:
pg_dump -U postgres -t OldSchema."TableToCopy" --inserts OldDatabase > Table.sql
我还尝试从表复制到文件并使用 COPY 导入,对我来说它有效。
您确定您的客户端和服务器数据库编码是 UTF8 吗?
首先,将名为“x”的表从数据库“test”上的模式“public”导出到纯文本 SQL 文件:
pg_dump -U postgres -t public."x" test > x.sql
它创建包含以下内容的 x.sql 文件:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: x; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE x (
first_two_letters character(3)
);
ALTER TABLE public.x OWNER TO postgres;
--
-- Data for Name: x; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY x (first_two_letters) FROM stdin;
سر
\.
--
-- PostgreSQL database dump complete
--
其次,导入:
psql -U postgres -d test -f x.sql