Postgres 命令行语句:INSERT 抱怨不存在的列

发布于 2024-11-08 04:28:09 字数 1559 浏览 0 评论 0原文

我有以下脚本,它通过 ssh 连接到远程服务器,并在其 Postgresql 数据库上发出 SQL 语句:

#!/bin/bash
db_query() {
    app_server="$1"
    sql_stmt="$2"
    psql_cmd="psql -d vdc --pset tuples_only -c '$sql_stmt'"
    rows_count=`ssh $app_server "sudo su - postgres -c \"$psql_cmd\""`

    echo "DB_QUERY: rows_count = $rows_count"
}

现在我尝试向下表发出 SELECT 和 INSERT 语句:

CREATE TABLE pb_properties
(
  c_name character varying(255) NOT NULL,
  c_value character varying(255),
  CONSTRAINT pb_properties_pkey PRIMARY KEY (c_name)
)

如果我执行 SELECT 语句,此函数可以正常工作:

#!/bin/bash
source db_query.sh
db_query staging "SELECT * FROM pb_properties;"

>> Output: DB_QUERY: rows_count =  support-email | [email protected]

但是如果我执行 INSERT 语句,它不起作用:

#!/bin/bash
source db_query.sh
db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('support-email', '[email protected]');"

>> Output:
>>ERROR:  column "testname" does not exist
>>LINE 1: ...SERT INTO pb_properties (c_name, c_value) VALUES (testname, ...
                                                             ^
>>DB_QUERY: rows_count =

现在如何使用 db_query 函数执行成功的 INSERT 语句?我已经尝试过以多种方式屏蔽我尝试插入的值,但它们都不起作用。我想这与通过 SSH 运行 sql 命令和我使用的不同引号 `、" 和 ' 的组合有关。

I have the following script, which connects through ssh to a remote server and issues an SQL statement on it's Postgresql database:

#!/bin/bash
db_query() {
    app_server="$1"
    sql_stmt="$2"
    psql_cmd="psql -d vdc --pset tuples_only -c '$sql_stmt'"
    rows_count=`ssh $app_server "sudo su - postgres -c \"$psql_cmd\""`

    echo "DB_QUERY: rows_count = $rows_count"
}

Now I'm trying to issue SELECT and INSERT statements to the following table:

CREATE TABLE pb_properties
(
  c_name character varying(255) NOT NULL,
  c_value character varying(255),
  CONSTRAINT pb_properties_pkey PRIMARY KEY (c_name)
)

This function works fine if I do a SELECT statement:

#!/bin/bash
source db_query.sh
db_query staging "SELECT * FROM pb_properties;"

>> Output: DB_QUERY: rows_count =  support-email | [email protected]

But it does not work if I do an INSERT statement:

#!/bin/bash
source db_query.sh
db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('support-email', '[email protected]');"

>> Output:
>>ERROR:  column "testname" does not exist
>>LINE 1: ...SERT INTO pb_properties (c_name, c_value) VALUES (testname, ...
                                                             ^
>>DB_QUERY: rows_count =

Now how can I do a successful INSERT statement with my db_query function? I already tried masking the values I tried to insert in many several ways, but none of them worked. I guess it has something to do with the combination of running the sql command through SSH and the different quotes i'm using `, " and '.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书信已泛黄 2024-11-15 04:28:09

您可以使用以下代码示例来消除对 tmp 文件的依赖:

echo "$sql_stmt" | ssh $app_server "sudo su - postgres -c '/usr/local/pgsql/bin/psql -A -t -U postgres $database'"

Here is a code sample you can use that removes the reliance of a tmp file:

echo "$sql_stmt" | ssh $app_server "sudo su - postgres -c '/usr/local/pgsql/bin/psql -A -t -U postgres $database'"
初雪 2024-11-15 04:28:09

好的,由于我无法使用问题中的 db_query 方法获取带单引号的 SQL 语句,因此我正在采取解决方法。在远程服务器上,我将 SQL 语句重定向到临时文件中。然后我可以让 psql 读取该文件,然后我就不必再关心引号了。

这是我的新 db_query 函数:

tmpfile=/tmp/pgquery

db_query() {
    app_server="$1"
    sql_stmt="$2"
    ssh $app_server "echo \"$sql_stmt\" > $tmpfile"
    psql_cmd="psql -d vdc --pset tuples_only -f $tmpfile"
    rows_count=`ssh $app_server "sudo su - postgres -c \"${psql_cmd}\""`

    echo "DB_QUERY: rows_count = $rows_count"
}

现在它适用于我最初的声明:

db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('testname', 'testvalue');"

无论如何,如果有人提示如何在没有临时文件的情况下使其工作,我很高兴听到它。

Ok, since I cannot get the SQL statement with single quotes working at all with the db_query method in my question, I'm doing a workaround. On the remote Server, I'm redirecting the SQL statement into a temporary file. Then I can let psql read that file, then I don't have to care about quotes anymore.

This is my new db_query function:

tmpfile=/tmp/pgquery

db_query() {
    app_server="$1"
    sql_stmt="$2"
    ssh $app_server "echo \"$sql_stmt\" > $tmpfile"
    psql_cmd="psql -d vdc --pset tuples_only -f $tmpfile"
    rows_count=`ssh $app_server "sudo su - postgres -c \"${psql_cmd}\""`

    echo "DB_QUERY: rows_count = $rows_count"
}

This now works with my initial statement:

db_query prestaging "INSERT INTO pb_properties (c_name, c_value) VALUES ('testname', 'testvalue');"

Anyway, if anybody has an hint how to get it working without a temporary file, I'd be glad to hear it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文