Open uniquejava opened 4 years ago
pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
application.yml
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: postgres
参考: Creating a Trigger in PostgreSQL
-- postgres triggers
-- see https://www.postgresqltutorial.com/creating-first-trigger-postgresql/
drop trigger if exists grade_changes on customer;
create or replace function create_event()
returns trigger as
$BODY$
begin
if new.grade <> old.grade then
insert into event(event_title, event_description, start_date, end_date, start_time, end_time,
is_full_day_event,
is_recurring, created_by,
created_date, parent_event_id)
values ('customer grade changed!', concat('from ', old.grade, ' to ', new.grade), now(), null,
null, null, 'N', 'N', 'cyper', now(), null);
end if;
return new;
end;
$BODY$
LANGUAGE plpgsql VOLATILE;
create trigger grade_changes
after update
on customer
for each row
execute procedure create_event();
another trigger
-- postgres triggers
-- see https://www.postgresqltutorial.com/creating-first-trigger-postgresql/
create or replace function create_event2()
returns trigger
language plpgsql
as
$$
declare v_post post%rowtype;
begin
select * into v_post from post where id = new.post_id;
if v_post.user_id = 2 then
insert into event(event_title, event_description, start_date, end_date, start_time, end_time,
is_full_day_event,
is_recurring, created_by,
created_date, parent_event_id)
values (v_post.title, concat('刚有人回复了你的帖子:',new.body), now(), null,
null, null, 'N', 'N', 'cyper', now(), null);
end if;
return new;
end;
$$;
drop trigger if exists comment_count_changes on comment;
create trigger comment_count_changes
after insert
on comment
for each row
execute procedure create_event2();
create table customer
(
id integer not null
constraint customer_pkey
primary key,
name varchar(100) not null,
grade char not null
);
alter table customer
owner to postgres;
create table event
(
id serial not null
constraint event_pkey
primary key,
event_title varchar(50) not null,
event_description varchar(500),
start_date date not null,
end_date date,
start_time time,
end_time time,
is_full_day_event char not null,
is_recurring char not null,
created_by varchar(10),
created_date timestamp default CURRENT_TIMESTAMP,
parent_event_id integer
);
alter table event
owner to postgres;
Note: Triggers 只能加载 updatable view 之上
Is a view in the database updatable?
Yes, they are updatable but not always. Views can be updated under followings:
If the view consists of the primary key of the table based on which the view has been created.
If the view is defined based on one and only one table.
If the view has not been defined using groups and aggregate functions.
If the view does not have any distinct clause in its definition.
If the view that is supposed to be updated is based on another view, the later should be updatable.
If the definition of the view does not have any sub queries.
方法一
SELECT setval('payments_id_seq', 21, true); # next value will be 22
SELECT setval('payments_id_seq', select max(id) from xxx, true); # next value will be max(id) + 1
true
表示在第二个参数21的基础上 +1, 是默认值。
方法二
ALTER SEQUENCE payments_id_seq RESTART WITH 22;
PostgreSQL Versions
Intellij IDEA
2020.1
对应的psql client为12/13docker compose (2020.11更新)
参考: Getting Started with PostgreSQL using Docker-Compose
.env文件
docker-compose.yaml文件
docker (以前写的太麻烦, 备份)
https://hub.docker.com/_/postgres/
默认创建的database name, user, password都是postgres
We have provided several options to the docker run command:
see: Don’t install Postgres. Docker pull Postgres