小编Dav*_*ave的帖子

如何在 Postgres 9.3 上安装/启用 uuid-ossp 扩展?

我在 Ubuntu Linux 14.04 上使用 Postgres 9.3。如何安装/启用 uuid-ossp 扩展?我首先验证了 contrib 包是否已安装……

root@prodbox:/home/rails/myproject# apt-get install postgresql-contrib
Reading package lists... Done
Building dependency tree       
Reading state information... Done
postgresql-contrib is already the newest version.
Run Code Online (Sandbox Code Playgroud)

然后我登录并尝试创建扩展...

root@prodbox:/home/rails/myproject# su - postgres
postgres@prodbox:~$ psql
psql (9.6.0, server 9.3.11)
Type "help" for help.

postgres=# CREATE EXTENSION "uuid-ossp";
ERROR:  could not open extension control file "/usr/share/postgresql/9.3/extension/uuid-ossp.control": No such file or directory
Run Code Online (Sandbox Code Playgroud)

我都没有想法了。我错过了什么?

postgresql postgresql-9.3 uuid postgresql-extensions

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

查找列不包含“空格”的行

我正在使用 Postgres 9.5。我想搜索我的名称列不包含空格的行。不过,我对如何为您定义空间有点模糊。我以为它只是我键盘上的空格键,所以我跑了:

.... where name not like '% %';
Run Code Online (Sandbox Code Playgroud)

但后来我得到了一些这样的结果:

 | JASON FALKNER
Run Code Online (Sandbox Code Playgroud)

这对我来说确实是一个空间,但可能还有其他一些事情正在发生。有没有更好的方法可以扫描我的名称列不包含空格的行?

使用正则表达式,not (name ~ '\s')仍然返回看起来有空格的列。

使用:

select cast(name as bytea) ... where name not like like '% %';
Run Code Online (Sandbox Code Playgroud)

回来:

\x4a41534f4ec2a0424c414b45
Run Code Online (Sandbox Code Playgroud)

但是,我仍然有点不清楚我如何使用这些数据来确定如何从我的结果中筛选空间。

我试过了where not (name ~ '[[:space:]]')',它返回“JASON BLAKE”与上面相同的字节序列,\x4a41534f4ec2a0424c414b45

postgresql pattern-matching postgresql-9.5

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

psql 没有提示我输入密码,然后拒绝访问

我在 Debian Linux 上使用 Postgres 9.4。我用一个用户创建了一个数据库,cindex可以访问该数据库。然而,当我尝试在命令行登录时,我什至没有提示输入密码:

myuser@myuserserver:~ $ psql -Ucindex cindex
psql: FATAL:  Peer authentication failed for user "cindex"
Run Code Online (Sandbox Code Playgroud)

我还需要做什么来启用用户?下面你可以看到我已经设置的权限:

postgres@myuserserver:~$ psql
psql (9.4.13)
Type "help" for help.

postgres=# GRANT SELECT ON ALL TABLES IN SCHEMA cindex TO cindex;
GRANT
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 cindex    | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =T/postgres          +
           |          |          |             |             | postgres=CTc/postgres+
           |          | …
Run Code Online (Sandbox Code Playgroud)

postgresql permissions psql password

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

为什么 Postgres 不遵守“log_filename”规定的我的日志文件名约定?

我在 Ubuntu 14.04 上使用 Postgres 9.5。我在 /etc/postgresql/9.5/main/postgresql.conf 文件中设置了这些设置

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
                                        # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                                        # begin with 0 to use octal notation
log_truncate_on_rotation = on           # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # …
Run Code Online (Sandbox Code Playgroud)

postgresql configuration disk-space log files

5
推荐指数
1
解决办法
1155
查看次数

在 Postgres 中,设置 log_rotation_size 但我的日志仍然没有旋转

我在 Ubuntu 14.04 上使用 Postgres 9.5。我想在日志达到特定大小时旋转它们。所以我编辑了 /etc/postgresql/9.5/main/postgresql.conf 并设置了这个

log_rotation_size = 50MB                # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.
Run Code Online (Sandbox Code Playgroud)

然后我重新启动了我的服务器

sudo /etc/init.d/postgresql restart
Run Code Online (Sandbox Code Playgroud)

但是在运行了一些操作之后,我的磁盘空间用完了,并且发现我的日志根本没有轮换……

myuser@mymachine:~$ ls -al /var/log/postgresql/postgresql-9.5-main.log
-rw-r----- 1 postgres postgres 3165773943 Dec 29 18:34 /var/log/postgresql/postgresql-9.5-main.log
Run Code Online (Sandbox Code Playgroud)

我还需要做什么才能开始日志轮换?

postgresql size disk-space log

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

如何基于列串联构建 PostgreSQL ORDER BY 表达式?

我正在使用 Postgres 9.5。我想为一系列连接的字符串编写一个 order by 表达式,但我不确定这是否可能或如何做到。我拼凑了以下内容

myproject=> SELECT  "my_objects".* 
            FROM "my_objects" 
              left join addresses on my_objects.address_id = addresses.id 
            WHERE (my_objects.name ILIKE '%my_object%' 
              AND EXISTS (SELECT * 
                          FROM my_object_times 
                          WHERE my_object_times.my_object_id = my_objects.id)) 
            order by lower(addresses.city) || "," || addresses.state_id; 

ERROR:  column "," does not exist 
LINE 1: ...id = my_objects.id)) order by lower(addresses.city) || "," || add...
Run Code Online (Sandbox Code Playgroud)

但正如您所看到的,有一个错误抱怨语法。我正在尝试做的事情是否可能,如果可以的话,我如何编写基于列串联的 order by 子句?

postgresql order-by concat postgresql-9.5

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

在 Postgres 中,如何调整“pgsql_tmp”设置?

我在 Ubuntu 14.04 上使用 Postgres 9.5.4。我的 Postgres 数据位于单独的磁盘分区上,该分区已满。

myuser@myproject:~$ df -h /mnt/volume-nyc1-01/
Filesystem Size Used Avail Use% Mounted on
/dev/sda 99G 93G 413M 100% /mnt/volume-nyc1-01
Run Code Online (Sandbox Code Playgroud)

我想从分区中删除一些数据,但这变得很有挑战性。特别是,当我运行一些查询时,我得到这样的结果

myproject_production=> select count(*) FROM my_object_times rt1, my_object_times rt2 where rt1.my_object_id = rt2.my_object_id and rt1.name = rt2.name and rt1.time_in_ms = rt2.time_in_ms and rt1.id > rt2.id;;
ERROR:  could not write block 52782 of temporary file: No space left on device
Run Code Online (Sandbox Code Playgroud)

我想释放一些临时空间,以便我可以运行查询并确定需要删除哪些数据。我的另一个分区上还有一些其他可用空间。如何将 pgsql_tmp 变量指向那里,以便我可以运行我需要的查询?

编辑:

由于符号链接选项似乎是侵入性最小的,所以我尝试了一下,设置如下

myuser@myproject:~$ sudo ls -al /mnt/volume-nyc1-01/postgresql/9.5/main/base/pgsql_tmp
lrwxrwxrwx 1 root root 14 Apr …
Run Code Online (Sandbox Code Playgroud)

postgresql disk-space temporary-tables

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

无需超级用户权限即可导出 Postgres 数据

我在 CentOS 7 上使用 Postgres 14。我想导出单个表的数据,但我没有超级用户权限。毫不奇怪,我收到此错误

myapp=> COPY myapp_currencyprice to '/tmp/prices.csv' DELIMITER ',' CSV HEADER;
ERROR:  must be superuser or a member of the pg_write_server_files role to COPY to a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
Run Code Online (Sandbox Code Playgroud)

对于没有超级用户权限的用户导出表数据(用于稍后导入)的正确方法是什么?

postgresql export table

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

如何在 Postgres 转储文件中禁用外键约束?

我正在使用 Postgres 9.5。我为我的三个表创建了一个 INSERT 转储文件。为了避免出现外键错误,我将以下内容添加到文件的顶部和底部(“...”是所有插入的位置)...

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

SET search_path = public, pg_catalog;
ALTER TABLE addresses DISABLE TRIGGER ALL;
ALTER TABLE table1 DISABLE TRIGGER ALL;
ALTER TABLE table2 DISABLE TRIGGER ALL;

…

ALTER TABLE addresses ENABLE TRIGGER ALL;
ALTER TABLE table1 ENABLE TRIGGER ALL;
ALTER TABLE table2 ENABLE TRIGGER ALL;
Run Code Online (Sandbox Code Playgroud)

但是,即使我运行这个文件,我也会收到类似的错误

ERROR:  insert or update …
Run Code Online (Sandbox Code Playgroud)

postgresql foreign-key insert dump

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

Postgres 中有没有更简洁的方法来提取字符串的一部分?

我正在使用 Postgres 9.5。我有一个表,其中有一列记录 URL。有时 URL 具有查询字符串,有时则没有。我想提取 URL,减去任何查询字符串,所以我想出了:

select substring(url, 0, case position('?' in url) when 0 then length(url)+1 else position('?' in url) end) 
from article;
Run Code Online (Sandbox Code Playgroud)

这看起来有点罗嗦,我想知道是否有更简洁的方法来做到这一点。我的表列是类型TEXT

postgresql select string substring

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