为了方便学习postgres数据库特性,本片文章中的postgres搭建在docker中,至于如何在docker中安装postgres,可以先打开https://registry.hub.docker.com/_/postgres,其中有具体的安装步骤。
我的安装命令是:
docker run --name test-postgres -p 5432:5432 -e POSTGRES_PASSWORD="your secret" -d postgres
数据连接工具是:
IDEA Database
创建数据库命令:
create database test;
创建表:
drop table if exists person;
create table person (
id serial not null ,
name varchar(20) not null default '',
primary key (id)
);
添加注释:
comment on table person is '这是一个人名表';
comment on column person.id is '自增ID';
comment on column person.name is '人物名称';
添加索引:
create index name_index on person(name);
创建模式:
drop schema if exists department;
create schema department;
模式:类似于数据库下的public,包含视图,索引,数据类型,函数和操作符等,使用多模式,可以在不同模式下建立相同的对象名称,可以将数据库对象进行逻辑分组,这样容易管理。
删除模式
drop schema department;空模式
drop schema department cascade;删除模式以及包含的所有对象。
插入数据
insert into person(name)values('Jason');
清空并重置表
truncate person restart identity;