标签: postgresql

如何找到 PostgreSQL 的数据目录?

我忘记了上次是如何启动 PostgreSQL 的(那是几个月前),我不记得数据目录位于何处。该postgres命令似乎需要数据目录的位置。

如果有帮助,我在 MacOsX 上。

/usr/local/postgres 在我的 Mac 上不存在。

使用下面提供的答案,我发现它在这里:

/usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

postgresql mac-os-x

162
推荐指数
6
解决办法
22万
查看次数

如何从 PostgreSQL 中获取当前数据库的名称?

\c <database_name>在 PostgreSQL 中使用将连接到指定的数据库。

如何确定当前数据库的名称?

进入:

my_db> current_database();
Run Code Online (Sandbox Code Playgroud)

产生:

ERROR:  syntax error at or near "current_database"
LINE 1: current_database();
Run Code Online (Sandbox Code Playgroud)

postgresql psql

143
推荐指数
4
解决办法
18万
查看次数

SQL:选择除某些列之外的所有列

有没有办法访问SELECT表中的所有列,除了特定的列?从表中选择所有非 blob 或非几何列会非常方便。

就像是:

SELECT * -the_geom FROM segments;
Run Code Online (Sandbox Code Playgroud)
  • 我曾经听说这个功能被故意排除在SQL 标准之外,因为更改向表中添加列会改变查询结果。这是真的?论证有效吗?
  • 有没有解决方法,尤其是在 PostgreSQL 中?

postgresql sql-standard

140
推荐指数
5
解决办法
14万
查看次数

PostgreSQL 多列唯一约束和 NULL 值

我有一张如下表:

create table my_table (
    id   int8 not null,
    id_A int8 not null,
    id_B int8 not null,
    id_C int8 null,
    constraint pk_my_table primary key (id),
    constraint u_constrainte unique (id_A, id_B, id_C)
);
Run Code Online (Sandbox Code Playgroud)

我想(id_A, id_B, id_C)在任何情况下都与众不同。所以下面的两个插入肯定会导致错误:

INSERT INTO my_table VALUES (1, 1, 2, NULL);
INSERT INTO my_table VALUES (2, 1, 2, NULL);
Run Code Online (Sandbox Code Playgroud)

但它没有按预期运行,因为根据文档,两个NULL值不会相互比较,因此两个插入都没有错误地通过。

我怎么能保证我的唯一约束,即使id_C可以NULL在这种情况下?实际上,真正的问题是:我可以在“纯 sql”中保证这种唯一性,还是必须在更高级别(在我的情况下为 java)来实现它?

postgresql null constraint unique-constraint

137
推荐指数
3
解决办法
13万
查看次数

优化对一系列时间戳的查询(两列)

我在 Ubuntu 12.04 上使用 PostgreSQL 9.1。

我需要在一个时间范围内选择记录:我的表time_limits有两个timestamp字段和一个integer属性。我的实际表中还有其他列与此查询无关。

create table (
   start_date_time timestamp,
   end_date_time timestamp, 
   id_phi integer, 
   primary key(start_date_time, end_date_time,id_phi);
Run Code Online (Sandbox Code Playgroud)

该表包含大约 200 万条记录。

像下面这样的查询花费了大量的时间:

select * from time_limits as t 
where t.id_phi=0 
and t.start_date_time <= timestamp'2010-08-08 00:00:00'
and t.end_date_time   >= timestamp'2010-08-08 00:05:00';
Run Code Online (Sandbox Code Playgroud)

所以我尝试添加另一个索引 - PK的倒数:

create index idx_inversed on time_limits(id_phi, start_date_time, end_date_time);
Run Code Online (Sandbox Code Playgroud)

我的印象是性能有所提高:访问表中间记录的时间似乎更合理:介于 40 到 90 秒之间。

但是对于时间范围中间的值,它仍然是几十秒。在针对表格末尾时(按时间顺序),还有两次。

explain analyze第一次尝试得到这个查询计划:

 Bitmap Heap Scan on time_limits  (cost=4730.38..22465.32 rows=62682 width=36) (actual time=44.446..44.446 rows=0 loops=1)
   Recheck …
Run Code Online (Sandbox Code Playgroud)

postgresql index optimization explain postgresql-9.1

129
推荐指数
2
解决办法
13万
查看次数

PostgreSQL 中 LIKE、SIMILAR TO 或正则表达式的模式匹配

我必须编写一个简单的查询,在其中查找以 B 或 D 开头的人名:

SELECT s.name 
FROM spelers s 
WHERE s.name LIKE 'B%' OR s.name LIKE 'D%'
ORDER BY 1
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法重写它以提高性能。所以我可以避免or和/或like

postgresql index regular-expression pattern-matching string-searching

123
推荐指数
3
解决办法
18万
查看次数

如何从 PostgreSQL 获取当前的 unix 时间戳?

Unix 时间戳是自 UTC 时间 1970 年 1 月 1 日午夜以来的秒数。

如何从 PostgreSQL 获取正确的 unix 时间戳?

currenttimestamp.comtimestamp.1e5b.de相比时,我没有从 PostgreSQL 得到预期的时间:

这将返回正确的时间戳:

SELECT extract(epoch from now());
Run Code Online (Sandbox Code Playgroud)

虽然这不会:

SELECT extract(epoch from now() at time zone 'utc');
Run Code Online (Sandbox Code Playgroud)

我住在 UTC +02 时区。从 PostgreSQL 获取当前 unix 时间戳的正确方法是什么?

这将返回正确的时间和时区:

SELECT now();
              now
-------------------------------
 2011-05-18 10:34:10.820464+02
Run Code Online (Sandbox Code Playgroud)

另一个对比:

select now(), 
extract(epoch from now()), 
extract(epoch from now() at time zone 'utc');
              now              |    date_part     |    date_part
-------------------------------+------------------+------------------
 2011-05-18 10:38:16.439332+02 | 1305707896.43933 | 1305700696.43933
(1 …
Run Code Online (Sandbox Code Playgroud)

postgresql timestamp

119
推荐指数
2
解决办法
21万
查看次数

测量 PostgreSQL 表行的大小

我有一个 PostgreSQL 表。select *很慢,但又select id好又快。我认为可能是行的大小非常大并且需要一段时间来运输,或者可能是其他一些因素。

我需要所有字段(或几乎所有字段),因此仅选择一个子集不是一个快速解决方案。选择我想要的字段仍然很慢。

这是我的表架构减去名称:

integer                  | not null default nextval('core_page_id_seq'::regclass)
character varying(255)   | not null
character varying(64)    | not null
text                     | default '{}'::text
character varying(255)   | 
integer                  | not null default 0
text                     | default '{}'::text
text                     | 
timestamp with time zone | 
integer                  | 
timestamp with time zone | 
integer                  | 
Run Code Online (Sandbox Code Playgroud)

文本字段的大小可以是任意大小。但是,在最坏的情况下,不会超过几千字节。

问题

  1. 有什么关于这叫“疯狂低效”的吗?
  2. 有没有办法在 Postgres 命令行中测量页面大小来帮助我调试?

postgresql performance size disk-space postgresql-performance

119
推荐指数
5
解决办法
10万
查看次数

PostgreSQL 不在 Mac 上运行

错误全文如下:

psql:无法连接到服务器:没有这样的文件或目录。服务器是否在本地运行并接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的连接?

这是我第二次在 Mac 上通过 Homebrew 设置 Postgresql,我不知道发生了什么。以前,它一直在工作。在某些时候,我一定是输入了一个把事情搞砸的命令。我不知道。现在,每当我从命令行输入 SQL 命令时,我都会收到上述消息。我已经运行了一个命令来检查服务器是否正在运行,但显然不是。如果我尝试使用

$ postgres -D /usr/local/pgsql/data

我收到以下错误:

postgres 无法访问服务器配置文件 "/usr/local/pgsql/data/postgresql.conf": No such file or directory

我已经通过 Homebrew 卸载并重新安装了 Postgresql,但问题仍然存在。我完全不知道如何让这个工作。任何帮助,将不胜感激。

postgresql mac-os-x

111
推荐指数
6
解决办法
24万
查看次数

Postgres 更新...限制 1

我有一个 Postgres 数据库,其中包含有关服务器集群的详细信息,例如服务器状态(“活动”、“待机”等)。活动服务器在任何时候都可能需要故障转移到备用服务器,我不在乎特别使用哪个备用服务器。

我想要一个数据库查询来更改备用服务器的状态 - 只有一个 - 并返回要使用的服务器 IP。选择可以是任意的:因为服务器的状态随着查询而改变,所以选择哪个备用数据库并不重要。

是否可以将我的查询限制为一次更新?

这是我到目前为止所拥有的:

UPDATE server_info SET status = 'active' 
WHERE status = 'standby' [[LIMIT 1???]] 
RETURNING server_ip;
Run Code Online (Sandbox Code Playgroud)

Postgres 不喜欢这样。我可以做些什么不同的事情?

postgresql concurrency update queue

107
推荐指数
1
解决办法
10万
查看次数