3

我有一个sql脚本在create_database_function.sql文件中定义一个函数:

CREATE OR REPLACE FUNCTION increment_display_count(in_host_id int, in_host_owner_id int, in_date_ date) RETURNS void as $$
BEGIN
    UPDATE display_count set display_count = display_count + 1 
    WHERE host_id = in_host_id AND host_owner_id = in_host_owner_id AND date_ = in_date_;
    IF FOUND THEN
        RETURN;
    END IF;
    BEGIN
        INSERT INTO display_count (host_id, host_owner_id, date_, display_count) VALUES (in_host_id, in_host_owner_id, in_date_, 1);
    EXCEPTION WHEN OTHERS THEN
        UPDATE display_count set display_count = display_count + 1
                WHERE host_id = in_host_id AND host_owner_id = in_host_owner_id AND date_ = in_date_;
    END;
    RETURN;
END;
$$
LANGUAGE plpgsql;

我想在Windows上执行。为此,我运行一个常用命令:

psql -h localhost -U postgres -d database -f create_database_function.sql

但是这个脚本给了我大量的语法错误。一个小时的谷歌搜索没有结果。但我继续修改这个脚本,最终发现了问题。

解决方案是在所有分号;符号前加上反斜杠\

虽然这解决了问题,但它引入了另一个问题。我和另一个人一起做这个项目。但他在 Linux 上工作。在他的情况下,脚本应该\在分号之前没有。

那么,为什么我需要在 Windows 上;添加前缀呢?\这可以以某种方式避免或以其他方式完成吗?

我用谷歌搜索了很多,没有发现任何类似的问题。


更新

我使用\;而不是时的输出;

C:\Xubuntu_shared\pixel\pixel\src\main\scripts>psql -h localhost -U postgres -d
pixel_test -f create_database_function.sql
Password:
CREATE FUNCTION

当我在没有反斜杠转义的情况下执行脚本时出现错误的输出:

C:\Xubuntu_shared\pixel\pixel\src\main\scripts>psql -h localhost -U postgres -d
pixel_test -f create_database_function.sql
Password:
psql:create_database_function.sql:4: ERROR:  unterminated dollar-quoted string a
t or near "$$
BEGIN
    UPDATE display_count set display_count = display_count + 1
    WHERE host_id = in_host_id AND host_owner_id = in_host_owner_id AND date_ =
in_date_;" at character 121
psql:create_database_function.sql:6: ERROR:  syntax error at or near "IF" at cha
racter 5
psql:create_database_function.sql:7: ERROR:  syntax error at or near "IF" at cha
racter 9
psql:create_database_function.sql:9: ERROR:  syntax error at or near "INSERT" at
 character 19
psql:create_database_function.sql:12: ERROR:  syntax error at or near "EXCEPTION
" at character 5
psql:create_database_function.sql:13: WARNING:  there is no transaction in progr
ess
COMMIT
psql:create_database_function.sql:14: ERROR:  syntax error at or near "RETURN" a
t character 5
psql:create_database_function.sql:15: WARNING:  there is no transaction in progr
ess
COMMIT
psql:create_database_function.sql:17: ERROR:  unterminated dollar-quoted string
at or near "$$
LANGUAGE plpgsql;" at character 1

其他可能重要的信息:

create_database_function.sql编码是UTF-8,没有BOM。行尾是 Windows 格式。


更新 2

版本

pixel=> SELECT version();
                           version
-------------------------------------------------------------
 PostgreSQL 9.2.3, compiled by Visual C++ build 1600, 32-bit
(1 row)


pixel=>

更新 3

命令的输出output of select name, setting, source from pg_settings where source <> 'default';

Oleg@OLEG-PC /C/Xubuntu_shared/pixel/pixel/src/main/scripts (pixel-dev2)
$ psql -U postgres
Password:
Welcome to psql.exe 7.4.6, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

Warning: Console codepage (866) differs from windows codepage (1251)
         8-bit characters will not work correctly. See PostgreSQL
         documentation "Installation on Windows" for details.

postgres=# select name,setting,source from pg_settings where source <> 'default'
;
            name            |                       setting
   |        source
----------------------------+---------------------------------------------------
---+----------------------
 config_file                | C:/Program Files/PostgreSQL/9.2/data/postgresql.co
nf | override
 data_directory             | C:/Program Files/PostgreSQL/9.2/data
   | override
 DateStyle                  | ISO, MDY
   | configuration file
 default_text_search_config | pg_catalog.english
   | configuration file
 hba_file                   | C:/Program Files/PostgreSQL/9.2/data/pg_hba.conf
   | override
 ident_file                 | C:/Program Files/PostgreSQL/9.2/data/pg_ident.conf
   | override
 lc_collate                 | C
   | override
 lc_ctype                   | C
   | override
 lc_messages                | C
   | configuration file
 lc_monetary                | C
   | configuration file
 lc_numeric                 | C
   | configuration file
 lc_time                    | C
   | configuration file
 listen_addresses           | *
   | configuration file
 log_destination            | stderr
   | configuration file
 log_line_prefix            | %t
   | configuration file
 log_timezone               | Europe/Moscow
   | configuration file
 logging_collector          | on
   | configuration file
 max_connections            | 100
   | configuration file
 max_stack_depth            | 2048
   | environment variable
 port                       | 5432
   | configuration file
 server_encoding            | UTF8
   | override
 shared_buffers             | 4096
   | configuration file
 TimeZone                   | Europe/Moscow
   | configuration file
 transaction_deferrable     | off
   | override
 transaction_isolation      | read committed
   | override
 transaction_read_only      | off
   | override
 wal_buffers                | 128
   | override
(27 rows)
4

1 回答 1

1

对于那些可能遇到这个罕见问题的人。

它与 Postgres 服务器和客户端版本不匹配有关。我正在使用版本的服务器和版本的9.2.3客户端7.4.6

重要的是要提到 Windows 的 Postgres 安装程序已经包含psql客户端。所以没有必要再安装一个。

我不记得为什么我安装了一个单独的客户端,但我猜是因为psql没有从控制台启动。我认为它可以通过在安装 PosrgreSQL 后重新启动 Windows 来解决(因为这种操作系统解决了很多问题)或手动添加psql.exe到环境路径变量的路径。

因此,如果您遇到同样的问题,请检查服务器和客户端的版本。如果它们不匹配,请将路径环境变量设置为原始 PosgreSQL 交付的正确客户端。

于 2013-04-22T08:28:56.033 回答