cacode commited on
Commit
d8d83be
·
verified ·
1 Parent(s): 4cdf901

Update hf-entrypoint.sh

Browse files
Files changed (1) hide show
  1. hf-entrypoint.sh +8 -11
hf-entrypoint.sh CHANGED
@@ -28,12 +28,11 @@ if [ ! -s "${PGDATA}/PG_VERSION" ]; then
28
  run_as_postgres initdb -D "${PGDATA}" >/dev/null
29
  fi
30
 
31
- # 关键:把 unix socket 放到 /tmp,避免 /run/postgresql 不存在导致启动失败
32
  run_as_postgres pg_ctl -D "${PGDATA}" \
33
  -o "-c listen_addresses=127.0.0.1 -c port=5432 -c unix_socket_directories=/tmp" \
34
  -w start
35
 
36
- # 创建户和库幂等——强制走 TCP
37
  run_as_postgres psql -h 127.0.0.1 -p 5432 -v ON_ERROR_STOP=1 --username postgres --dbname postgres <<SQL
38
  DO \$\$
39
  BEGIN
@@ -46,15 +45,13 @@ END
46
  \$\$;
47
  SQL
48
 
49
- run_as_postgres psql -h 127.0.0.1 -p 5432 -v ON_ERROR_STOP=1 --username postgres --dbname postgres <<SQL
50
- DO \$\$
51
- BEGIN
52
- IF NOT EXISTS (SELECT FROM pg_database WHERE datname = '${POSTGRES_DB}') THEN
53
- CREATE DATABASE ${POSTGRES_DB} OWNER ${POSTGRES_USER};
54
- END IF;
55
- END
56
- \$\$;
57
- SQL
58
 
59
  export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DB}"
60
  export POSTGRES_URL="${DATABASE_URL}"
 
28
  run_as_postgres initdb -D "${PGDATA}" >/dev/null
29
  fi
30
 
 
31
  run_as_postgres pg_ctl -D "${PGDATA}" \
32
  -o "-c listen_addresses=127.0.0.1 -c port=5432 -c unix_socket_directories=/tmp" \
33
  -w start
34
 
35
+ # 1) 角色可 DO允许
36
  run_as_postgres psql -h 127.0.0.1 -p 5432 -v ON_ERROR_STOP=1 --username postgres --dbname postgres <<SQL
37
  DO \$\$
38
  BEGIN
 
45
  \$\$;
46
  SQL
47
 
48
+ # 2) 数据库不能放 DO,改成 shell 判断
49
+ DB_EXISTS=$(run_as_postgres psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -tAc \
50
+ "SELECT 1 FROM pg_database WHERE datname='${POSTGRES_DB}'")
51
+
52
+ if [ "${DB_EXISTS}" != "1" ]; then
53
+ run_as_postgres createdb -h 127.0.0.1 -p 5432 -U postgres -O "${POSTGRES_USER}" "${POSTGRES_DB}"
54
+ fi
 
 
55
 
56
  export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DB}"
57
  export POSTGRES_URL="${DATABASE_URL}"