我有一个可以在本地运行的现有 Rails 7/PostgreSQL 应用程序,并且当前部署在 Heroku 上。我正在将其迁移到运行 Ubuntu 22 LTS(最初是 Ubuntu 20 LTS)的 VPS,但是当我尝试运行迁移(直接或通过 Capistrano)时,它们失败并出现错误:
PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist
第一个(十个!)迁移失败了,而不是重写历史和 insert/modify an early migration ,我通过手动创建 pgcrypto 扩展在服务器上修复了它:
myApp$ sudo su - postgres
postgres$ psql
postgres=# \c myapp_production
myapp_production=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION
myapp_production=# exit
据我记得,我没有执行任何操作来在 Mac 上启用扩展,我只是运行 rails new myapp --database=postgresql
并使用 create_table :people, id : :uuid 在我的第一次迁移中执行 |t|
。
为什么我需要在 Ubuntu 上创建/启用此扩展,而不是 macOS 或 Heroku?
在我的 Mac 上,我使用 Postgres 14.5(psql (PostgreSQL) 14.5,Homebrew)。在 Ubuntu 22 LTS 服务器上,我还使用 Postgres 14.5(psql (PostgreSQL) 14.5、Ubuntu 14.5-0ubuntu0.22.04.1)。
当我第一次注意到它时,我在服务器上使用了不同版本的 PostgreSQL(psql (PostgreSQL) 12.12、Ubuntu 12.12-0ubuntu0.20.04.1)。
根据Heroku Postgres web dashboard我在 Heroku 上使用 Postgres 13.9。
更奇怪的是,当我检查开发和生产中该数据库的已安装扩展时,pgcrypto 甚至没有列出:
# macOS using psql
myapp_development=# \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)
# Heroku using `heroku pg:psql`
myapp::DATABASE=> \dx
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+------------------------------
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(1 row)
我的 :uuid
列在这两个平台上运行得怎么样!?
最佳答案
解释毕竟很简单:
gen_random_uuid()
已添加到 Postgres 13 的主发行版中。(使用 SELECT version();
检查 Postgres 版本)
与服务器操作系统无关。
这也与交互终端的版本无关psql (可能与服务器版本不同)。
release notes Postgres 13 的:
Add function
gen_random_uuid()
to generate version-4 UUIDs (Peter Eisentraut)Previously UUID generation functions were only available in the external modules uuid-ossp and pgcrypto.
关于ruby-on-rails - 为什么新服务器上有 "PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist"而旧服务器上没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74527555/