postgres命令

1. mac postgres 安装

dmg包安装即可。

2.   命令

命令作用
psql -U username -d dbname -h 127.0.0.1
例子:

/Library/PostgreSQL/9.4/bin/psql -U postgres


/Library/PostgreSQL/9.4/bin/psql -U postgres -d testdb


登录/连接数据库
\l 显示数据库
\c dbname;
\d    显示表

\d   表    显示表字段

例子:

postgres=# \c testdb

You are now connected to database "testdb" as user "postgres".

testdb=# \d

           List of relations

 Schema |    Name    | Type  |  Owner   

--------+------------+-------+----------

 public | company    | table | postgres

 public | department | table | postgres

(2 rows)


testdb=# \d company;

       Table "public.company"

 Column  |     Type      | Modifiers 

---------+---------------+-----------

 id      | integer       | not null

 name    | text          | not null

 age     | integer       | not null

 address | character(50) | 

 salary  | real          | 

Indexes:

    "company_pkey" PRIMARY KEY, btree (id)

连接数据库
创建表:

CREATE TABLE test(id int, text VARCHAR(50));
INSERT INTO test(id, text) VALUES(1, 'sdfsfsfsdfsdfdf');
SELECT FROM test WHERE id = 1;
UPDATE test SET text = 'aaaaaaaaaaaaa' WHERE id = 1;
DELETE FROM test WHERE id = 1;
DROP TABLE test;
DROP DATABASE dbname;
简单例子

select pg_size_pretty(pg_total_relation_size('company'));

指定表大小

select pg_size_pretty(pg_database_size('testdb'));

指定数据库大小
select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size from pg_database; 查看所有数据库大小
  
  
  
  
  
  
  

3.  创建主键并设置自动递增的三种方法 

--方法一
create table test_a 
(
  id serial,
  name character varying(128),
  constraint pk_test_a_id primary key( id)
); 

NOTICE:  CREATE TABLE will create implicit sequence "test_a_id_seq" for serial column "test_a.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pk_test_a_id" for table "test_a"
CREATE TABLE


--方法二
create table test_b
(
  id serial PRIMARY KEY,
  name character varying(128)
); 

NOTICE:  CREATE TABLE will create implicit sequence "test_b_id_seq" for serial column "test_b.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_b_pkey" for table "test_b"
CREATE TABLE


--方法三
create table test_c 
(
  id integer PRIMARY KEY,
  name character varying(128)
);  
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_c_pkey" for table "test_c"
CREATE TABLE


CREATE SEQUENCE test_c_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
    
alter table test_c alter column id set default nextval('test_c_id_seq');


     很明显从上面可以看出,方法一和方法二只是写法不同,实质上主键都通过使用 serial 类型来实现的,
使用serial类型,PG会自动创建一个序列给主键用,当插入表数据时如果不指定ID,则ID会默认使用序列的
NEXT值。    
    
    方法三是先创建一张表,再创建一个序列,然后将表主键ID的默认值设置成这个序列的NEXT值。这种写法
似乎更符合人们的思维习惯,也便于管理,如果系统遇到sequence 性能问题时,便于调整 sequence 属性;

--比较三个表的表结构

三个表表结构一模一样, 三种方法如果要寻找差别,可能仅有以下一点,
当 drop 表时,方法一和方法二会自动地将序列也 drop 掉, 而方法三不会。



4.  常用数据类型


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多则惑少则明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值