postgresql

insertion of nested array of custom in table for postgres

核能气质少年 提交于 2021-01-07 01:23:45
问题 command = ( CREATE TYPE belongings AS ( item TEXT, quantity INTEGER ) CREATE TYPE student AS ( name TEXT, id INTEGER, bag belongings[] ) CREATE TABLE studentclass( date DATE NOT NULL, time TIMESTAMPTZ NOT NULL, PRIMARY KEY (date, time), class student ) ) Can i ask how to do insert for this in postgres psycog2? thank you. When i put the insert as insert_sql = "INSERT INTO studentclass (date, time, class) VALUES (%s,%s,%s)" error output is DETAIL: Cannot cast type text[] to belongings[] in

postgresql 数据库的备份和还原

隐身守侯 提交于 2021-01-06 15:48:27
第一步:通过 cmd 进入到postgresql 安装目录的 bin 下 : windows : cd C:\PostgreSQL\pg95\bin ubuntu : cd /etc/postgresql/9.5/main 第二步:备份数据库 C:\PostgreSQL\pg95\bin>pg_dump -h 164.82.233.54 -U postgres databasename > C:\databasename.bak -h:数据库服务器地址; -p:数据库端口号; -U:U 大写,表示用户名; -d:数据库名称; -f:把备份文件放在哪里; 第三步:还原数据库 ( 前提:你要备份的数据库软件里面必须先创建一个数据库 ) windows: psql -h localhost -U postgres -d new_db -f "C:/emoneysit.bak" ubuntu: psql -h localhost -U emmweb -d emmweb < /home/jianghai/Desktop/emmweb.bak -h:数据库服务器地址; -p:数据库端口号; -U:U 大写,表示用户名; -d:数据库名称; -f:备份文件路径以及备份文件名称; 执行命令:备份完成; 转自: https://blog.csdn.net/jinjianghai/article

Postgresql创建用户以及配置相应的权限

夙愿已清 提交于 2021-01-06 14:27:37
1) 启动数据库服务,使用超级用户 postgres 创建应用用户 appuser,赋权createdb、login,appuser 的密码设置为 1qaz@WSX,并体现到.pgpass 文件中, 以便 appuser 免密登录,appuser 用户的密码在 2022 年 05 月 01 日之前是有效的。 2) 创建属主为appuser 的表空间并命名为appuser,指向/appuser(若没有此目录请自行创建并管理权限); 3) 创建appdb 数据库,owner 是appuser(要求appdb 数据库要在exam 表空间内),并要求实现: a.回收 appdb 中的 public schema 上的 create object 权限。 b. 以 appuser 用户在 appdb 数据库中创建名为 appuser 的 schema c. 以 appuser 用户在 appdb 数据库中创建 app 表(id int),app 表的 schema是 appuser 其他的非超级用户不能 connect 到 appdb 数据库中 4) 自行建立 readonlyuser 用户,要求如下: a. readonlyuser 能连接到 appdb 中 b. 密码设置为 1qaz@WSX,并体现到.pgpass 文件中,以便 readonlyuser 免密登录 c.

PostgreSQL数据库备份、还原

陌路散爱 提交于 2021-01-06 14:26:53
ostgresql的常见备份方式:SQL转储 以下通过实例来讲解PostgreSQL的SQL转储 (一)pg_dump 1.创建数据库 createdb pg 2.连入数据库pg psql pg 3.创建测试表,插入数据 create table pg_test(a int); insert into pg_test(a) values(1); insert into pg_test(a) values(2); 4.查看数据 select from tb; 5.备份 pg_dump pg > /usr/local/pgsql/backup/pg.dmp 6.删除数据库pg dropdb pg 7.创建新数据库(恢复之前需创建数据库) createdb pg 8.恢复数据 psql pb < /usr/local/pgsql/backup/pg.dmp 9.查看数据是否回复 select from tb; 至此,数据已成功恢复! 注:pg_dump可以对针对单表或者多表进行备份 如:pg_dump databasename –t tablename1 –t tablename2 >filename (二)pg_dumpall pg_dump只能备份单个数据库,而且恢复的时候需要创建空数据库。pg_dumpall可以备份所有数据库,并且备份角色、表空间。 1.创建数据库

Postgresql修改集群(实例)的配置

房东的猫 提交于 2021-01-06 14:26:19
1) 修改集群(实例)的配置,要求如下: a. 运行日志路径/pglog b. 运行日志格式修改为'csvlog' c. 每天生成一个新的日志文件 d. 不限制单个日志文件的大小 e. PostgreSQL 的运行 log 日志需要保留最近 1 个月的 log 日志文件。 f. 最多允许 100 个非超级用户的连接连入数据库中。 g. 运行日志内容中要体现时间戳(带毫秒,不适用 unix 时间戳),用户名,数据库名称和进程 ID,依照上述顺序 h. 需要监听的地址设置为*, i. 开启全页写配置 j. 数据库用户的密码加密算法修改为 scram-sha-256,并确保数据库用户能正常登录。 k. 配置好.pgpass 文件,以便 postgres 用户可以免密输入 pg10@data01 ~]$ psql -d postgres -U postgres psql (10.14) Type "help" for help. postgres=# alter system set logging_collector=on; ALTER SYSTEM postgres=# \q [pg10@data01 ~]$ su - root Password: Last login: Mon Jan 4 22:27:02 CST 2021 on pts/1 [root@data01 ~]#

为PostgreSQL的表自动添加分区

◇◆丶佛笑我妖孽 提交于 2021-01-06 14:24:21
作者:乐途 笔名:lottu 个人简介:目前在一家游戏公司从事数据库架构设计和开发,曾主导公司去“ O ”的相关工作;个人博客:https://www.cnblogs.com/lottu PostgreSQL引进“分区”表特性,解放了之前采用“表继承”+“触发器”来实现分区表的繁琐、低效。而添加分区,都是手动执行SQL。 演示目的 : 利用python来为PostgreSQL的表自动添加分区。 python版本 : python3 + pip3 install psycopg2 一、配置数据源 database.ini 文件:记录数据库连接参数 [adsas] host=192.168.1.201 database=adsas user=adsas password=adsas123 port=5432 [test] host=192.168.1.202 database=adsas user=adsas password=adsas123 port=5432 二、config 脚本 config.py 文件:下面的 config() 函数读取 database.ini 文件并返回连接参数。config() 函数位于config.py文件中 #!/usr/bin/python3 from configparser import ConfigParser def config

PostgreSQL硬件相关性能调优

。_饼干妹妹 提交于 2021-01-06 05:22:21
PostgreSQL Hardware Performance Tuning Bruce Momjian P OSTGRE SQL is an object-relational database developed on the Internet by a group of developers spread across the globe. It is an open-source alternative to commercial databases like Oracle and Informix. POSTGRESQL was originally developed at the University of California at Berkeley. In 1996, a group began development of the database on the Internet. They use email to share ideas and file servers to share code. POSTGRESQL is now comparable to commercial databases in terms of features, performance, and reliability. It has transactions, views

postgresql 备份之-pg_basebackup

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-05 14:40:17
(一)创建基础备份 1 、配置可以基于时间点的备份与恢复(point-in-time recovery (PITR)): 1.1 postgresql.conf 中配置 wal_level = replica # used to be "hot_standby" in older versions 9.6 default value minimal max_wal_senders = 10 # at least 2, better at least 2 archive_mode = on archive_command = 'cp %p /archive/%f' (也可使用rsync , scp ,需确保返回值为0,才被认为是归档成功了) 1.2 pg_hba.conf 的配置 # replication privilege. local replication postgres trust host replication postgres 127.0.0.1/32 trust host replication postgres ::1/128 trust 1.3配置完后可以进行备份数据库 export PGPASSWORD=passowrd nohup pg_basebackup -D /postgresql/backup -U repuser -h 1922.168.1.1

Unique value constraint across multiple columns

纵然是瞬间 提交于 2021-01-05 11:50:02
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm

Unique value constraint across multiple columns

落爺英雄遲暮 提交于 2021-01-05 11:49:05
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm