标签: postgresql

PostgreSQL,在数字 JSON 数组中按值查找元素

我有一个表定义为:

create table dummy (jdata jsonb);
Run Code Online (Sandbox Code Playgroud)

我插入了以下两行:

insert into dummy values ('["dog","cat","elephant","waffle"]');
insert into dummy values ('[1,2,3,4]');
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 jsonb?&运算符,它可以让您提出问题“所有这些键/元素字符串都存在吗?”

使用字符串字段的示例有效:

select * from dummy where jdata ?& array['cat','dog'];
            jdata                 
--------------------------------------
["dog", "cat", "elephant", "waffle"]
(1 row)
Run Code Online (Sandbox Code Playgroud)

但是,尝试使用包含数字的数组来执行此操作是行不通的:

select * from dummy where jdata ?& array['1','2'];
  jdata 
  -------
(0 rows)

select * from dummy where jdata ?& array['1','2'];
 jdata 
 -------
 (0 rows)

select * from dummy where jdata ?& array[1,2];
ERROR:  operator does not exist: jsonb ?& integer[]
LINE …
Run Code Online (Sandbox Code Playgroud)

postgresql array json

6
推荐指数
1
解决办法
4195
查看次数

删除wal文件后如何解决postgresql问题?

我在我的 postgresql 配置上打开了 archive_mode 以测试备份服务器。由于 wal 文件占用了大量磁盘空间,因此在测试后我将其关闭并删除了 wal 文件。当我尝试重新启动 postgresql 时,出现以下错误。

 root@hooshang:/etc/postgresql/9.1/main# /etc/init.d/postgresql restart
 * Restarting PostgreSQL 9.1 database server
 * The PostgreSQL server failed to start. Please check the log output:
 2014-10-16 13:15:28 IRST LOG:  database system was shut down at 2014-10-15 15:51:53 IRST
 2014-10-16 13:15:28 IRST LOG:  could not open file "pg_xlog/00000001000007DC00000037" (log file 2012, segment 55): No such file or directory
 2014-10-16 13:15:28 IRST LOG:  invalid primary checkpoint record
 2014-10-16 13:15:28 IRST LOG:  could not open file …
Run Code Online (Sandbox Code Playgroud)

postgresql backup checkpoint postgresql-9.1 write-ahead-logging

6
推荐指数
1
解决办法
3万
查看次数

在 Postgres 中获取一天中的秒数

在 Java 中,我们使用 Joda-Time 来获取一天中的秒数作为 int 值(尽管是日期):

date: 10-10-2014 00:00:30 -> second 30 of day
date: 11-10-2014 00:01:30 -> second 90 of day
date: 12-10-2014 00:02:00 -> second 120 of day
Run Code Online (Sandbox Code Playgroud)

有没有办法在 PostgresSQL 中做同样的事情?

postgresql datetime

6
推荐指数
2
解决办法
5876
查看次数

Postgresql 和 UTF8 到 Latin1 的转换?

我们有一个 Postgresql 数据库,其中数据在 Latin1 中。显然有人将一些 UTF8 编码的数据加载到其中,因为当我从某些列中选择数据时,我得到如下内容:

SELECT symptoms from client
- -
"huvudvärke"
- -
Run Code Online (Sandbox Code Playgroud)

其中“ä”应该是“ä”。我可以用替换来处理它,即:

UPDATE client SET symptoms=replace(symptoms, 'ä', 'ä');
Run Code Online (Sandbox Code Playgroud)

我尝试了几个我找到的解决方案,比如使用convert和 ,convert_to但这使数据看起来像"huvudv\303\244rke".

我的问题有现有的解决方案吗?

编辑:

SELECT symptoms, symptoms::bytea from client
- -
"huvudvärke";"huvudv\303\203\302\244rke" 
- -
Run Code Online (Sandbox Code Playgroud)

在上面的 UPDATE 之后,查询症状::bytea 输出:

SELECT symptoms, symptoms::bytea from client
- -
"huvudvärke";"huvudv\303\244rke" 
- -
Run Code Online (Sandbox Code Playgroud)

编辑2:

select version();
"PostgreSQL 9.2.9, compiled by Visual C++ build 1600, 64-bit"

show client_encoding; --I'm using pgAdmin III 1.18.1
"UNICODE"

show server_encoding;
"UTF8" …
Run Code Online (Sandbox Code Playgroud)

postgresql bytea

6
推荐指数
1
解决办法
4万
查看次数

唯一字段的 Guid 与整数 ID

这真的是一个笼统的问题。

假设我有一个单一写入 DB 和多个读取 DB 复制从属的复制模式 - 我不必将 GUID 作为我的唯一 ID 字段。这是一个正确的假设吗?

如果我有多个数据库实例,其中所有实例都可写并且它们之间同步,则唯一 ID 字段中的 GUID 是强制性的。这也是正确的吗?

我正在 Amazon RDS 上设置 PostgreSQL。

谢谢。

postgresql uuid amazon-rds

6
推荐指数
1
解决办法
885
查看次数

Postgres - GROUP BY 的计数

我正在尝试确定图像数据库中至少贡献了五张图像的贡献者总数。以下查询将按贡献者提供图像计数,但这只是难题的一部分。我相信这对于比我有更多知识的人来说很简单:)

SELECT count(*) AS i_count,contributor_id FROM images GROUP BY contributor_id ORDER BY i_count DESC

postgresql count

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

检查是否存在连续的日期间隔

我在 PostgreSQL 中有一个表,它描述了一些具有开始日期和结束日期的事件:

CREATE TABLE my_table
(
  event_id serial NOT NULL,
  start_date timestamp without time zone NOT NULL,
  end_date timestamp without time zone NOT NULL
)
Run Code Online (Sandbox Code Playgroud)

单个事件可能与前一个和下一个事件重叠。在下表中,4 个事件中的前 3 个形成连续的时间间隔:

1   '2015-04-02 22:09:03'   '2015-04-02 22:19:05'
2   '2015-04-02 22:17:38'   '2015-04-02 22:27:38'
3   '2015-04-02 22:25:21'   '2015-04-02 22:36:23'
4   '2015-04-02 22:45:23'   '2015-04-02 22:55:23'
Run Code Online (Sandbox Code Playgroud)

是否可以编写一个查询来检查两个给定日期之间是否存在连续的日期间隔?

我想要一些类似的东西:

select ...
from my_table
where start_date > '2015-04-02' and end_date < '2015-04-06'
Run Code Online (Sandbox Code Playgroud)

postgresql window-functions gaps-and-islands

6
推荐指数
1
解决办法
7138
查看次数

调试 PostgreSQL 序列化失败

我想我们的PostgreSQL 9.4的事务级数据库迁移READ COMMITTED要么REPEATABLE READSERIALIZABLE。在任何一种情况下,我都会遇到一组新的格式错误:

(for both)
ERROR:  could not serialize access due to concurrent update
(just for SERIALIZABLE)
ERROR:  could not serialize access due to read/write dependencies among transactions
Run Code Online (Sandbox Code Playgroud)

在阅读了 SSI 上的 wiki 页面文档后,我彻底了解了可能导致这些错误的错误条件、如何处理它们,甚至是避免它们的最佳实践。

但是,我看不到从 PostgreSQL 可以提供的任何调试输出或任何调试信息中确定导致它们的数据依赖性的方法。有没有办法从数据库中获取这些信息,要么在回滚时执行额外的查询,要么通过某种日志机制?

有了这些信息,我就可以进行应用程序级别的更改(锁定、不同的查询等),从而消除一些数据竞争以避免过多的回滚。

postgresql isolation-level serialization postgresql-9.4

6
推荐指数
1
解决办法
1492
查看次数

无法使用 pg_ctl 运行 postgres

我正在阅读一本 postgresql 书(从新手到专业版的 PostgreSQL),它说您可以通过调用作为安装一部分的 postgres 二进制文件来启动 postmaster 守护进程:

/usr/lib/postgresql/9.3/bin/postgres "-D" "/var/lib/postgresql/9.3/main" "-c" "config_file=/etc/postgresql/9.3/main/postgresql.conf"
Run Code Online (Sandbox Code Playgroud)

请注意,我们必须将数据目录和配置作为参数传递给 postgres 二进制文件。

然后这本书继续说 pg_ctl 可执行文件,当通过 apt-get 安装时,它也恰好存储在 /usr/lib/postgresql/9.3/bin 目录中,用于简化任务。但我无法让 pg_ctl 运行:

$ /usr/lib/postgresql/9.3/bin/pg_ctl -D "/var/lib/postgresql/9.3/main" start
server starting
postgres@estonia:/usr/lib/postgresql/9.3/bin$ postgres cannot access the server configuration file "/var/lib/postgresql/9.3/main/postgresql.conf": No such file or directory
Run Code Online (Sandbox Code Playgroud)

它说找不到配置文件,但是这里没有办法指定配置文件:

$ /usr/lib/postgresql/9.3/bin/pg_ctl start "-D" "/var/lib/postgresql/9.3/main" "-c" "config_file=/etc/postgresql/9.3/main/postgresql.conf"
pg_ctl: too many command-line arguments (first is "start")
Try "pg_ctl --help" for more information.
Run Code Online (Sandbox Code Playgroud)

如何让 pg_ctl 运行?

postgresql

6
推荐指数
2
解决办法
3万
查看次数

PostgreSQL 备份和恢复,大小不同

我的 postgreSQL 中有一个 Wordpress 数据库,在 phppgadmin 中,该数据库的大小为 273 MB。

我跑sudo -u postgres pg_dump -Fp -f wordpress.sql wordpress做数据库备份,结果是一个只有 18MB 的文件。

我已将原始数据库的名称更改为 Wordpress2,然后:

我创建了一个名为 Wordpress 的新数据库并进行了恢复sudo -u postgres psql wordpress < wordpress.sql ,结果是一个大小为 19 MB 的数据库,是否缺少某些内容?

下面是phppgadmin的打印,wordpress作为导入的备份,wordpress2作为原始数据库。 管理员

自动吸尘器

postgres=# select setting from pg_settings where name = 'autovacuum';
 setting
---------
 on
(1 row)
Run Code Online (Sandbox Code Playgroud)

索引空间

postgres=# select pg_size_pretty(sum(pg_relation_size(indexrelid))) from pg_index;
     pg_size_pretty
    ----------------
     2600 kB
    (1 row)
Run Code Online (Sandbox Code Playgroud)

原始数据库大小

postgres=# select pg_size_pretty(pg_database_size('wordpress2'));
 pg_size_pretty
----------------
 273 MB
(1 row)
Run Code Online (Sandbox Code Playgroud)

已恢复

postgres=# select pg_size_pretty(pg_database_size('wordpress'));
 pg_size_pretty …
Run Code Online (Sandbox Code Playgroud)

postgresql

6
推荐指数
1
解决办法
8789
查看次数