`

PostgreSQL

阅读更多
   
PostgreSQL Commands  QuickStart:
http://www.thegeekstuff.com/2009/04/15-practical-postgresql-database-adminstration-commands/
http://www.linuxweblog.com/postgresql-reference
#查看 PostgreSQL 命令行工具的帮助,最有用的一个!!!!
\?
#重启 postgresql 服务 (ubuntu下)
$ sudo service postgresql restart
#登陆 PostgreSQL 命令行 (postgres 为其内置用户;这个默认的用户没有密码)
su - postgres
psql
#查看 SQL 语句的帮助
\h
#退出 PostgreSQL 命令行
\q
#列出所有库
\l
#切换数据库、用户等
\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]
#查看当前库
select current_database();
#新建库
create database dbName;
#删除库
drop database dbName;
#列出当前库中的所有表
\d
#连接远程库 ( https://www.blog.akendo.eu/enable-remote-access-postgresql/ )
#如果连接被拒绝,参见 http://www.cyberciti.biz/faq/postgresql-remote-access-or-connection/
psql -h hostOfRemoteDB -U userOfRemoteDB -d nameOfRemoteDB
#查看所有用户
select * from pg_user;
查看当前用户是谁
select current_user;
#创建新用户
create user scott password 'tiger';
create user "www-data" password 'www-data'; //用户名中含特殊字符需用双引号引起来
#修改用户密码
\password
alter user postgres with password 'postgres';
#使某一用户拥有superuser角色,superuser对应的所有权限自然也赋给了该用户(当前用户需有权限做这个事,如默认的postgres)
alter user "www-data" with superuser;
# update query with join on two tables (http://stackoverflow.com/questions/2815953/update-query-with-join-on-two-tables)
UPDATE address 
SET cid = customers.id
FROM customers 
WHERE customers.id = address.id



PostgreSQL 9.2 Documentation:
http://www.postgresql.org/docs/9.2/static/index.html
引用
关于 Data Types - Numeric Types - Serial Types:
postgresql的Serial数据类型最终是通过sequence实现的,即:
CREATE TABLE tablename (
    colname SERIAL
);
完全等价与:
CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;



当使用某一用户连接提示 "Peer authentication failed for user" 时:
http://blog.deliciousrobots.com/2011/12/13/get-postgres-working-on-ubuntu-or-linux-mint/
引用
正确答案在该博文的评论里:
You don't necessarily have to change the configuration. If you add -h 127.0.0.1 to the psql command it will connect through TCP instead of a Unix socket, in which case password authentication is used.



shell脚本中执行psql,怎么免去输入密码的麻烦(psql command 没有提供输入密码的参数):
http://stackoverflow.com/questions/6405127/how-do-i-specify-a-password-to-psql-non-interactively
#!/bin/bash

echo "localhost:*:*:www-data:www-data" > $HOME/.pgpass
echo "`chmod 0600 $HOME/.pgpass`"

psql -h localhost -U www-data -w -d email -c "delete from email_logging where receive_date <= current_timestamp - interval'10 days'"





CRUD

alter table tab_name add unique (col_name);

postgresql paging:
select user_id,email from email_queue order by id limit 5000
select user_id,email from email_queue order by id offset 5001

INSERT INTO core_email_queue(user_id,email)
 (SELECT user_id,email
    FROM email_queue
   order by id limit 5000 ) 

   INSERT INTO target_email_queue(user_id,email)
 (SELECT user_id,email
    FROM email_queue
   order by id offset 5001) 

Remove duplicate:
----去除按某列dup_col的重复
delete from dup_table
where id not in
(
	 select min(dup.id)
     from dup_table as dup
     group by dup.dup_col
);
-----去除按col1,col2,col3的组合作为重复判断标准的重复
delete from dup_table
where id not in
(
	 select min(dup.id)
     from dup_table as dup
     group by dup.col1, dup.col2, dup.col3
);




remote server copy:
引用
lee@ubuntu:~$ scp /home/qa.txt root@192.168.1.159:/root
lee@ubuntu:~$ ssh root@192.168.1.159
domU-00-16-3e-00-00-31 ~ # psql -U www-data zhoukan
//注意postgresql copy 命令的 from 路径不能是相对路径,如(当前用户root)这里写成 ‘~/qa.txt’ 是不行的!
zhoukan=# copy target_email_queue(user_id, email) from '/root/qa.txt' with csv;
qa.txt为以下格式的标准csv文档:
20463047,"bestyyz@hotmail.com"
4823001,"xueye_0511@163.com"
19544966,"lei_designcaa@163.com"
20443601,"ne.dingding@gmail.com"
19824195,"312743454@qq.com"
18351606,"kebei123@126.com"
787387,"wztwn@vip.sina.com"
6525245,"bsspirit@163.com"
13635170,"javahuangchengzhao@163.com"
8550177,"sinber@126.com"




Postgresql 中的 Unique Index 和常见的用来加唯一约束的 unique constraint 的区别:
http://www.postgresql.org/docs/9.2/static/indexes-unique.html
引用
PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table. The index covers the columns that make up the primary key or unique constraint (a multicolumn index, if appropriate), and is the mechanism that enforces the constraint.
The preferred way to add a unique constraint to a table is ALTER TABLE ... ADD CONSTRAINT. The use of indexes to enforce unique constraints could be considered an implementation detail that should not be accessed directly. One should, however, be aware that there's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index.(每个参与了 unique constraint 的列都会被隐式建立各自独立的 index,不需要再显式为这些列建立 index)




postgresql 中若使用了 distinct select, 则 order by 的列必须出现在 select 中,,否则会报 “for SELECT DISTINCT, ORDER BY expressions must appear in select list”,原因见:
http://stackoverflow.com/questions/12693089/pgerror-select-distinct-order-by-expressions-must-appear-in-select-list
引用
event_id |        start_time
----------+------------------------
    0     | Mar 17, 2013  12:00:00
    1     |  Jan 1, 1970  00:00:00
    1     | Aug 21, 2013  16:30:00
    2     |  Jun 9, 2012  08:45:00
Now you want to grab a list of distinct event_ids, ordered by their respective start_times. But where should 1 go? Should it come first, because the one tuple starts on Jan 1, 1970, or should it go last because of the Aug 21, 2013?
分享到:
评论

相关推荐

    PostgreSQL中文手册9.2

    PostgreSQL中文学习手册 PostgreSQL PostgreSQL PostgreSQL学习手册 学习手册 学习手册 (数据表 数据表 ) 4 一、表的定义: 一、表的定义: 一、表的定义: . 4 PostgreSQL PostgreSQL PostgreSQL学习手册 学习手册...

    DB2到GreenPlum/PostgreSQL的转换指南

    ### DB2到GreenPlum/PostgreSQL的转换指南 #### 1. 引言 ##### 1.1 目的 本指南旨在帮助用户理解从DB2迁移到GreenPlum或PostgreSQL过程中所涉及的关键技术和注意事项。由于这两种数据库系统之间存在显著差异,因此...

    PostgreSQL(postgresql-13.5.tar.bz2)

    PostgreSQL(postgresql-13.5.tar.bz2) PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。...

    PostgreSQL(postgresql-13.5.tar.gz)

    PostgreSQL(postgresql-13.5.tar.gz) PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES...

    Postgresql-10安装包

    **PostgreSQL 10 安装指南** PostgreSQL 是一款强大的开源关系型数据库管理系统,具有高度的稳定性和可扩展性,被广泛应用于各种规模的企业和项目。本文将详细介绍如何使用提供的 PostgreSQL-10 安装包进行安装,...

    Navicat for PostgreSQL(PostgreSQL数据库管理)V11.0.10简体中文特别版

    Navicat for PostgreSQL是一套专为PostgreSQL设计的强大数据库管理及开发工具。它可以用于任何版本 7.5 或以上的 PostgreSQL 数据库服务器,并支持大部份 PostgreSQL最新版本的功能,包括触发器、函数、管理用户等。...

    postgresql ubantu离线安装版

    PostgreSQL是一种开源的对象关系型数据库管理系统(ORDBMS),它在Ubuntu操作系统上广泛使用,尤其在需要稳定性和高性能的环境中。Ubuntu离线安装版的PostgreSQL适用于那些没有互联网连接或者网络带宽有限的环境,...

    postgresql 12、15离线安装包

    PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性和可靠性而受到全球开发者的广泛赞誉。在标题和描述中提到的“postgresql 12、15离线安装包”指的是为这两个版本提供的安装程序,适用于没有互联网...

    PostgreSQL(postgresql-13.5-1-windows-x64.exe)

    PostgreSQL(postgresql-13.5-1-windows-x64.exe)适用于Windows x86-64 PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的...

    PostGreSQL安装部署系列:Centos 7.9 安装指定PostGreSQL-15版本数据库

    ### PostGreSQL在Centos 7.9上的安装与部署 #### 一、引言 在开始学习任何数据库之前,最重要的第一步就是安装部署一个可供学习和测试的环境。选择一个在业界广泛使用的操作系统版本,以及一个成熟稳定的数据库...

    连接postgresql数据库需要的jar包

    在Java编程中,连接到PostgreSQL数据库通常需要特定的驱动程序,这个驱动程序通常是以JAR(Java Archive)文件的形式存在。"连接postgresql数据库需要的jar包"指的是用于建立Java应用程序与PostgreSQL数据库之间通信...

    postgresql-42.5.0.jar

    postgresql-42.5.0.jar是Java上的一个驱动程序,用于连接PostgreSQL数据库并与其进行交互。它可以让Java程序员方便地使用PostgreSQL数据库,并提供了许多功能和工具,使程序员可以编写高效、稳定和高性能的应用程序...

    ruoyi-postgresql-flowable

    "ruoyi-postgresql-flowable" 是一个基于 PostgreSQL 数据库的 Ruoyi 前后端分离项目,它集成了 Flowable 工作流引擎,用于实现高效的企业级流程管理。这个项目旨在提供一个完整的解决方案,使开发人员能够快速构建...

    postgresql离线安装包及依赖库

    PostgreSQL是一种开源的对象关系型数据库管理系统(ORDBMS),它以其强大的功能、高度的稳定性以及对ACID(原子性、一致性、隔离性、持久性)事务的支持而受到广大开发者的欢迎。离线安装包通常是为了在没有网络连接...

    postgresql12主从集群安装

    【PostgreSQL 12主从集群安装】 在搭建PostgreSQL 12主从集群时,首先需要理解主从复制的概念。主从复制是数据库高可用性的一种常见解决方案,它允许数据从一个节点(主节点)实时同步到另一个或多个节点(从节点)...

    PostgreSQL(postgresql-14.2-2-windows-x64.exe)

    PostgreSQL(postgresql-14.2-2-windows-x64.exe),适用于Windows系统:PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象...

    postgresql PGCA 课程PPT01_postgresql_PGCA_PGCP_PGCM_课程全免费

    【PostgreSQL】是一种高度成熟且功能丰富的开源(对象-关系型)数据库管理系统,它在业界被认为是主流数据库之一,尤其在企业级应用中受到广泛青睐。PostgreSQL 的特性包括支持复杂查询、事务处理、多版本并发控制...

    PostgreSQL数据库内部原理剖析 PDF

    A Tour of PostgreSQL Internals.pdf Inside the PostgreSQL Query Optimizer.pdf Inside the PostgreSQL Shared Buffer Cache.pdf Internals Of PostgreSQL Wal.pdf PostgreSQL Internals Through Pictures.pdf ...

    PostgreSQL(postgresql-13.5-1-osx.dmg)

    PostgreSQL(postgresql-13.5-1-osx.dmg)适用于Mac OS X PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库...

    postgresql分布式安装部署

    ### PostgreSQL分布式安装部署详解 #### 一、概览 本文旨在详细介绍如何在Ubuntu 12.04系统上进行PostgreSQL 9.1数据库的分布式安装与部署。本教程不仅包含详细的步骤指导,还提供了架构原理图以及安装过程中的...

Global site tag (gtag.js) - Google Analytics