psql有一个-q/ --quiet选项(环境变量QUIET). pg_restore没有安静的选择.是否有任何方法可以pg_restore不冗长地显示它正在执行的SQL命令?
# e.g., here's the verbose output that I don't want to see:
$ pg_restore --cluster 8.4/mycluster mycluster.dump
---- PostgreSQL database dump
--
SET statement_timeout = 0;SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;SET check_function_bodies = false;
...
--
-- Name: data_src; Type: TABLE; Schema: public; Owner: postgres; Tablespace:--
CREATE TABLE data_src (
...
Run Code Online (Sandbox Code Playgroud) 这是我简单的匿名代码块:
do $$
declare foo varchar(50) := '';
begin
for a in
select a from (values('foo'), ('bar'), ('fooBar')) s(a)
loop
foo := a;
print foo;
end loop;
end;
$$;
Run Code Online (Sandbox Code Playgroud)
当我运行它:
psql -f test.sql
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
psql:test.sql:11: ERROR: loop variable of loop over rows must be a record or row variable or list of scalar variables
LINE 4: for a in
^
Run Code Online (Sandbox Code Playgroud) 在Linux中使用Psql时,如果我的SQL查询的结果包含许多列或长数据字符串,它将包装初始视图,只有当我滚动到侧面时它才会停止换行并在单独的行上显示每一行.
我已经尝试了各种\pset方案,如format unaligned,format aligned,format wrapped,columns 0,columns 1000,但似乎没有一个完全停止包装,除非我生成静态输出到文件.
如何将其设置为永不包装输出,同时仍然可滚动并使用默认的ascii表格式显示结果?
假设我在postgresql中创建了一个序列:
CREATE SEQUENCE my_seq;
Run Code Online (Sandbox Code Playgroud)
我将以下行存储在sql文件get_seq.sql中
SELECT last_value FROM my_seq;
$SUDO psql -q -d database_bame -f get_seq.sql
Run Code Online (Sandbox Code Playgroud)
如何将SELECT返回的int数字转换为bash并使用它?
这是从" 将PostgreSQL的PL/pgSQL输出保存到CSV文件 "的答案中的后续问题.
我需要使用psql的\copy命令编写客户端CSV文件.一个班轮工作:
db=> \copy (select 1 AS foo) to 'bar.csv' csv header
COPY 1
Run Code Online (Sandbox Code Playgroud)
但是,我有很长的查询,跨越几行.我不需要显示查询,因为我似乎无法在没有解析错误的情况下延伸过去一行:
db=> \copy (
\copy: parse error at end of line
db=> \copy ( \\
\copy: parse error at end of line
db=> \copy ("
\copy: parse error at end of line
db=> \copy "(
\copy: parse error at end of line
db=> \copy \\
\copy: parse error at end of line
Run Code Online (Sandbox Code Playgroud)
是否可以使用\copy跨越多行的查询?我在Windows上使用psql.
这是'我目前的状态.
Eonil=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+------------+-------+-------+------------+-------------
public | TestTable1 | table | Eonil | 8192 bytes |
(1 row)
Eonil=# \d+ TestTable1
Did not find any relation named "TestTable1".
Eonil=#
Run Code Online (Sandbox Code Playgroud)
有什么问题,如何查看表定义?
我试图psql使用批处理脚本执行此命令:
psql --host=localhost --dbname=<dbname> --port=<Port Number>
--username=<dbuser> --file=C:\PSQL_Script.txt --output=C:\PSQL_Output.txt
Run Code Online (Sandbox Code Playgroud)
问题是每次执行批处理脚本时都要求输入密码.如何通过批处理文件密码参数?
如何将psql的时区设置为默认值(US/Central)以外的其他值?这是我到目前为止所尝试的:
$ psql
psql (9.1.4, server 9.0.4)
...
$ psql -c 'show timezone'
TimeZone
------------
US/Central
$ psql --set=timezone=US/Eastern -c 'show timezone'
TimeZone
------------
US/Central
$ psql --variable=timezone=US/Eastern -c 'show timezone'
TimeZone
------------
US/Central
Run Code Online (Sandbox Code Playgroud)
编辑:我不想更改服务器时区,只是客户端.
编辑#2:我想要它以非交互模式.
当我使用psqlPostgreSQL的命令行工具时,在交互模式下,它将数据列为分页输出.
但是,因为我psql在一个可以自己处理长输出的终端应用程序中使用,所以我更愿意获得整个输出,而不需要使用恼人的--more--行进行分页.
有没有办法自定义分页行为psql?我试图LESS在周围的shell环境中设置cat,但这没有帮助.
有什么建议?
我有这个PL/pgSQL函数,它必须返回一些用户信息.
CREATE OR REPLACE FUNCTION my_function(
user_id integer
) RETURNS TABLE(
id integer,
firstname character varying,
lastname character varying
) AS $$
DECLARE
ids character varying;
BEGIN
ids := '';
--Some code which build the ids string, not interesting for this issue
RETURN QUERY
EXECUTE 'SELECT
users.id,
users.firstname,
users.lastname
FROM public.users
WHERE ids IN (' || ids || ')';
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我面临的问题是函数的结果是单列表,如下所示:
???????????????????????????
? ?my_function ?
???????????????????????????
? 1 ? (106,Ned,STARK) ?
? 2 ? (130,Rob,STARK) …Run Code Online (Sandbox Code Playgroud) postgresql ×10
psql ×10
bash ×1
client-side ×1
command-line ×1
connection ×1
function ×1
pager ×1
pg-restore ×1
plpgsql ×1
schema ×1
shell ×1
timezone ×1