标签: postgresql-10

Postgres Psycopg2创建表

我是Postgres和Python的新手.我试图创建一个简单的用户表,但我不知道它为什么不创建.错误消息没有出现,

    #!/usr/bin/python
    import psycopg2

    try:
        conn = psycopg2.connect(database = "projetofinal", user = "postgres", password = "admin", host = "localhost", port = "5432")
    except:
        print("I am unable to connect to the database") 

    cur = conn.cursor()
    try:
        cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    except:
        print("I can't drop our test database!")

    conn.close()
    cur.close()
Run Code Online (Sandbox Code Playgroud)

python database postgresql python-3.6 postgresql-10

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

Alter TYPE RENAME VALUE在Postgres 10中有效,但在Postgres 9.6中无效吗?

我正在使用Postgres 10.3。

基于这个问题,我使用:

ALTER TYPE name RENAME VALUE attribute_name TO new_attribute_name
Run Code Online (Sandbox Code Playgroud)

重命名枚举值。

但是我需要一个与Postgres 9.6一起使用的解决方案,该解决方案不需要pg_enum手动更新,因为它需要我没有的权限。

postgresql postgresql-9.5 postgresql-10

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

PostgreSQL负整数溢出

我在使用tinyint扩展时对Postgres进行了一些测试,当时我遇到了关于其范围的令人惊讶的事情.打字时,select -128::tinyint它给了我一条ERROR: tinyint out of range消息,这根本不是我所期待的.

假设负数应该比正的最大值(或单个字节整数为127)大1(或者更小)我认为这是扩展的错误,但是在尝试使用非扩展数字时,我发现完全相同的是发生.

select -32768::smallint - >超出范围

select -2147483648::integer - >超出范围

select -9223372036854775808::bigint - >超出范围

参照数字数据类型的文档(https://www.postgresql.org/docs/current/datatype-numeric.html)这些数字都应该是可能的-所有负数少了一个-32767,-2147483647,-9223372036854775807正常工作,所以我很好奇,为什么会这样,或者这甚至发生在其他人的副本上.

我尝试在ubuntu 16.x桌面上使用postgresql 10和postgresql 11.

postgresql postgresql-10 postgresql-11

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

PostgreSQL可以使用HASH排除约束以获得唯一性吗?

由于哈希值比冗长的文本小,在我看来,为了确保列的唯一性,它们可能比b树更受欢迎。

仅出于确保唯一性的目的,是否有任何理由在PG 10中不符合以下条件?

CREATE TABLE test (
  path ltree,
  EXCLUDE USING HASH ((path::text) WITH =)
);
Run Code Online (Sandbox Code Playgroud)

我认为哈希冲突是在内部处理的。否则将毫无用处。

我将使用GiST索引来增强查询。

postgresql indexing unique-index ltree postgresql-10

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

pg_dumpall 不提示密码

我们正在尝试自动备份集群的所有 PostgreSQL 数据库,我们决定使用“pg_dumpall”实用程序。但是我们找不到在不提示密码的情况下执行“pg_dumpall”的方法。我们正在使用 PostgreSQL-10。

我们正在尝试执行以下命令

pg_dumpall -U "username" -h "hostname" > "location"
Run Code Online (Sandbox Code Playgroud)

我们如何在不提示密码的情况下自动化 pg_dumpall ?请帮助我们解决这个问题。谢谢

postgresql postgresql-10 pg-dumpall

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

使用 bash 查找 PostgreSQL 数据库是否存在

我有一个 bash 函数,用于检查 PostgreSQL 数据库是否已存在。

我捕获输出。如果数据库存在,PostgreSQL 将返回数据库名称作为响应。

function is_database() {
    local database=$1
    local output=$(sudo -u postgres psql -c "SELECT datname FROM pg_catalog.pg_database WHERE datname=\"$database\";")
    if [[ $output = *"${1}"* ]]
    then
        return 0
    else
        return 1
    fi
}

is_database test
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

column "test" does not exist
Run Code Online (Sandbox Code Playgroud)

我不是在搜索表,而是在搜索数据库。

postgresql bash postgresql-10

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

在 Postgresql 的非易失性函数中不允许 CREATE TABLE AS

我在 postgresql 10 上写了这个方法:

create or replace function get_users()
  returns TABLE(user_idd uuid, device_idd text, shapee text , datee timestamp) AS
$$
begin
  create temp table lines as
    SELECT DISTINCT user_id, device_id from olocations;
  select uuid_generate_v4(),
         o1.user_id,
         o1.device_id,
         st_astext(ST_Collect(o1.shape)),
         date(o1.creation_date_time) as date
  from olocations o1
      inner join lines on o1. device_id = lines.device_id and o1.user_id = lines.user_id
    where o1.user_id = 'd0edfc59-9923-44c3-9c34-ef5aad3cb810'
    and o1.device_id = '89984320001811791540'
group by o1.user_id, o1.device_id, date
order by date ASC ;
DROP TABLE lines;
end
$$
LANGUAGE …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-10

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

使用 levenshtein() 函数优化 PostgreSQL 查询

我有一个包含大约 700 万条记录的表。该表有一个名字和姓氏列,我想使用 levenshtein() 距离函数进行搜索。

select levenshtein('JOHN', first_name) as fn_distance,
       levenshtein('DOE', last_name) as ln_distance,
       id,
       first_name as "firstName",
       last_name as "lastName"
  from person
 where first_name is not null
   and last_name is not null
   and levenshtein('JOHN', first_name) <= 2
   and levenshtein('DOE', last_name) <= 2
 order by 1, 2
 limit 50;
Run Code Online (Sandbox Code Playgroud)

上面的搜索很慢(4 - 5 秒),我可以做些什么来提高性能?应该在两列上创建索引,还是其他什么?

添加以下索引后:

create index first_name_idx on person using gin (first_name gin_trgm_ops);

create index last_name_idx on person using gin(last_name gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)

查询现在大约需要 11 秒。:(

新查询:

select similarity('JOHN', first_name) as fnsimilarity, …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-performance postgresql-10

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

Postgres pg_dump版本不匹配...即使版本相同

我正在尝试从Amazon RDS转储Postgres数据库,我最近更新到10.1.为此,我pg_dumpentrepreneedb.com(http://get.enterprisedb.com/postgresql/postgresql-10.1-1-linux-x64-binaries.tar.gz)下载10.1,但当我尝试使用它时,我得到了以下错误:

pg_dump: server version: 10.1; pg_dump version: 10.1
pg_dump: aborting because of server version mismatch
Run Code Online (Sandbox Code Playgroud)

这在RDS版本9.6和我使用pg_dump9.6(从同一位置下载)之前工作正常.

我尝试了更新版本的pg_dump(10.2和10.3),但得到了同样的错误(这是有道理的,因为那些较新的版本实际上与RDS版本不匹配).

我还检查了pg_dump我的服务器上没有安装其他设备(Ubuntu 15.04 - 我知道我应该更新......等待18.04),找到一个并删除它,但得到同样的错误.

这可能是什么问题?

postgresql pg-dump postgresql-10

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

在 Postgresql 中创建分区表

我正在尝试找到一个创建分区表的示例。我有一些包含许多元组的表,我可以根据一列的值对它们进行分类,但是,我只是找到使用范围和日期的示例(我的列是 varchar,在其他表中是 int/外键)。我正在尝试用这种技术来加快我的选择速度。

这是我的 CREATE 表之一(源列将用于对该表进行分区):

CREATE TABLE tb_hit_source (
Hit_SourceId bigserial NOT NULL,
Source varchar(50) NOT NULL,
UniqueId varchar(50) NOT NULL,
tb_hit_HitId int8 NOT NULL,
CONSTRAINT tb_hit_source_ak_1 UNIQUE (Source, tb_hit_HitId, UniqueId) NOT DEFERRABLE INITIALLY IMMEDIATE,
CONSTRAINT tb_hit_source_pk PRIMARY KEY (Hit_SourceId)
);
CREATE INDEX tb_hit_source_idx_1 on tb_hit_source (Source ASC);
CREATE INDEX tb_hit_source_idx_2 on tb_hit_source (tb_hit_HitId ASC);
ALTER TABLE tb_hit_source ALTER COLUMN Hit_SourceId SET DEFAULT nextval('"HitSourceId_seq_tb_hit_source"');;
Run Code Online (Sandbox Code Playgroud)

postgresql database-partitioning postgresql-10

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

尝试在PostgreSQL 10中选择数据时查询错误

我刚刚开始使用PostgreSQL并且遇到了一些问题.我正在使用Psycopg2并在AWS RDS上托管PostgreSQL数据库.这是我试图执行的查询:

cursor.execute('SELECT id FROM fqdn AS domain WHERE domain.fqdn IS {0}'.format(fqdn_str))
Run Code Online (Sandbox Code Playgroud)

fqdn_str是这样的域名:

https://www.example.com
Run Code Online (Sandbox Code Playgroud)

和fqdn表是这样的:

CREATE TABLE fqdn (
    id SERIAL PRIMARY KEY,
    fqdn TEXT NOT NULL,
    date_last_crawled TIMESTAMP WITH TIME ZONE NOT NULL,
        UNIQUE (fqdn)
);
Run Code Online (Sandbox Code Playgroud)

我得到的错误是这样的:

psycopg2.ProgrammingError: syntax error at or near "https"
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么,所以我希望得到一些帮助.如果您需要更多信息,请告诉我.谢谢.

编辑:

如果我将格式字符串更改为%s,如文档中所示,我会收到以下错误:

TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)

python postgresql postgresql-10

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

根据Postgres 10中JSON的特定值执行UPSERT

我有一个 Postgres 表messages如下:

Column   |           Type             | Collation | Nullable |                    
-----------+--------------------------+-----------+----------
 id        | integer                  |           | not null |
 message   | jsonb                    |           |          | 
 date      | timestamp with time zone |           | not null | 
Run Code Online (Sandbox Code Playgroud)
id | message                      | date
1  | {"name":"alpha", "pos":"x"}  | 2020-02-11 12:31:44.658667+00
2  | {"name":"bravo", "pos":"y"}  | 2020-02-11 12:32:43.123678+00
3  | {"name":"charlie", "pos":"z"}| 2020-02-11 12:38:37.623535+00
Run Code Online (Sandbox Code Playgroud)

我想做的是根据name键的值执行 UPSERT,即,如果存在具有相同值的插入name,则更新另一个值pos,否则创建一个新条目。

我做到了CREATE UNIQUE INDEX message_name ON messages((message->>'name'));

我在 …

sql postgresql json upsert postgresql-10

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