首页 > 解决方案 > 如何在 Postgres 上将 UUID 与非特权用户一起使用?

问题描述

How make a default value for uuid rows?,该uuid_generate_v4()功能仅在uuid-ossp启用扩展但无法启用的情况下起作用。

postgres=# CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION
postgres=# SELECT uuid_generate_v4();
           uuid_generate_v4           
--------------------------------------
 929d5e41-d7a8-408a-b0e9-feecf10d853d
(1 row)
...
demo=> select uuid_generate_v4();
ERROR:  function uuid_generate_v4() does not exist
LINE 1: select uuid_generate_v4();
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
demo=> CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
ERROR:  permission denied to create extension "uuid-ossp"
HINT:  Must be superuser to create this extension.

标签: dockerpostgresql-10

解决方案


最重要的是,它并没有说它不可用,但不允许使用非特权用户创建扩展。在 Docker 映像合成期间,您应该创建和/或启用所需的扩展。

官方的 postgres docker images 将执行放置在/docker-entrypoint-initdb.d/文件夹下的脚本。

如果您使用官方镜像作为基础镜像(推荐),您只需创建一个.sh文件,授予可执行标志(chmod 755),然后将其添加到/docker-entrypoint-initdb.d/文件夹中。

希望它会起作用:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname="$POSTGRES_DB"<<-EOSQL
   CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
EOSQL

推荐阅读