bingoohuang / blog

write blogs with issues
MIT License
178 stars 24 forks source link

Postgres #195

Open bingoohuang opened 3 years ago

bingoohuang commented 3 years ago
# ./docker-compose.yaml
version: '3.2'

services:
  pg:
    image: postgres:12.4
    ports:
      - 2001:5432
    volumes:
      - ./init-pg.sh:/docker-entrypoint-initdb.d/init-pg.sh
    environment:
      - POSTGRES_USER=pg
      - POSTGRES_PASSWORD=pg
      - POSTGRES_DB=pg
#!/bin/sh

# init-pg.sh

set -e

psql -v ON_ERROR_STOP=1 --username pg --dbname pg <<-EOSQL
    CREATE DATABASE pg;
    GRANT ALL PRIVILEGES ON DATABASE pg TO pg;
EOSQL
bingoohuang commented 3 years ago
-- manually input
CREATE TABLE video_sales (
    did      VARCHAR(40) NOT NULL DEFAULT '',
    number   INTEGER DEFAULT 0
);

insert into video_sales(did, number) values(null, 1);
-- [23502] ERROR: null value in column "did" violates not-null constraint 详细:Failing row contains (null, 1).
insert into video_sales(number) values(1);
-- [2021-04-23 09:24:44] 1 row affected in 7 ms

select * from video_sales;

-- auto-generated definition
create table video_sales
(
    did    varchar(40) default ''::character varying not null,
    number integer     default 0
);

alter table video_sales owner to pg;