1、在ubuntu 14.4上安装, sudo apt-get install postgresql
安装完成后,service postgresql status 看到服务已经运行。绑定的端口为5432。
2、sudo -i -u postgres 切换到postgres用户(postgresql默认会创建这个用户)
psql 进入 psql客户端。
\du 查看所有用户,\l 查看所有数据库,\q 退出 、
\c 显示当前以什么用户登陆到哪个数据库中 \c mydb切换数据库
\d 显示当前数据库有哪些表
\d mytable 显示表的定义
3、create database db22; //createdb dropdb这两个程序,就是对这个sql语句的封装
create table t1(name varchar(10), age int);
insert into t1 values('gao',10),('feng',11);
create schema vm; //schema是一个逻辑上的命名空间
create table vm.t1(name varchar(10), age int);
insert into vm.t1 values('gao',10),('feng',11);
select * from public.t1;//默认的schema 是public
select * from vm.t1;
4、用户和角色
PostgreSQL: Documentation: 14: 22.2. Role Attributes
一文全搞懂postgresql的权限_DBA攻坚之路的技术博客_51CTO博客
create role role1;//默认无login权限
create user user1; //默认有login权限
create role user2 login;//和user1完成相同。 用户和角色差别就是有没有登录权限。
grant role1 to user2;
alter database db1 owner to user2;
5、show databases;show tables;describe table_name;
5.1、相当与mysql的show databases;
select datname from pg_database;
5.2、相当于mysql的show tables;
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
public 是默认的schema的名字
5.3、相当与mysql的describe table_name;
SELECT column_name FROM information_schema.columns WHERE table_name ='table_name';
'table_name'是要查询的表的名字
6、postgresql同时也是一个面向对象数据库
所以支持表的继承关系,分区也是依赖继承实现的。
1). 创建"主表",所有分区都从它继承。
CREATE TABLE measurement ( --主表
city_id int NOT NULL,
logdate date NOT NULL,
peaktemp int,
);
2). 创建几个"子"表,每个都从主表上继承。通常,这些"子"表将不会再增加任何字段。我们将把子表称作分区,尽管它们就是普通的PostgreSQL表。
CREATE TABLE measurement_yy04mm02 ( ) INHERITS (measurement);
CREATE TABLE measurement_yy04mm03 ( ) INHERITS (measurement);
...
CREATE TABLE measurement_yy05mm11 ( ) INHERITS (measurement);
CREATE TABLE measurement_yy05mm12 ( ) INHERITS (measurement);
CREATE TABLE measurement_yy06mm01 ( ) INHERITS (measurement);
详见 https://www.cnblogs.com/stephen-liu74/archive/2012/04/27/2291814.html
pg数据库的(表收缩)(垃圾回收)(autovacuum)
PostgreSQL Autovacuum基础知识_kmblack1的博客-优快云博客
select n_dead_tup as "死元组数" from pg_stat_all_tables where relname='表名';
select autovacuum_count as "自动收缩的次数" from pg_stat_all_tables where relname='表名';
select last_autovacuum as "上次自动收缩时间" from pg_stat_all_tables where relname='表名';
安装官方的说明,删除记录时,只是标记一下,所以会产生垃圾数据。
修改时,可能会先删除,后增加。--估计修改的数据,大于预留的空间时才会发生。没有做实验验证。
实验验证,系统默认开启了autovacuum,删除数据后,会立即自动触发autovacuum。可以看到autovacuum_count 增加了。
alter user gg with password '12345678';
sh-4.4$ psql -U gg -d gaofeng -h 127.0.0.1
Password for user gg: 输入密码
psql (14.12)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
gaofeng=> \c
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
You are now connected to database "gaofeng" as user "gg".
gaofeng=>
gaofeng=> insert into t2 values('gao',10),('feng',11);
INSERT 0 2
gaofeng=> select * from t2;
name | age
------+-----
gao | 10
feng | 11
(2 rows)
gaofeng=> \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t1 | table | postgres
public | t2 | table | gg
(2 rows)
gaofeng=> insert into t1 values('gao'),('feng');
ERROR: permission denied for table t1
gaofeng=>
pg_dump -c --if-exists -d gaofeng -f gaofeng.sql
pg_dumpall -c --if-exists -f backup.dump