-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL #309
Comments
Cyper 实战Spring Boot Integrationpom.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 PostgreSQL Triggers参考: 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(); |
PostgreSQL DDLcreate 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 之上 Updatable viewIs a view in the database updatable? Yes, they are updatable but not always. Views can be updated under followings:
|
修改seq的起始值方法一 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
方法二
|
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
The text was updated successfully, but these errors were encountered: