如何利用Heroku的“postdeploy”脚本在预置的Heroku PostgreSQL数据库中创build表?
我正在Heroku上设置一个Web应用程序。 我希望别人能够自己使用它,所以我试图创build一个“部署到Heroku”button,以包含在我的存储库的自述文件。
跟着Heroku的文档1,2 ,我创build了一个app.json
文件,概述了Herokuconfiguration应用程序所需的所有内容。 这是我的app.json
文件的样子:
{ "name": "[title]", "author": "[author]", "description": "[desc]", "repository": "[https://github.com/[user]/[repo]", "logo": "[url]", "addons": [ "heroku-postgresql:hobby-dev", "wwwhisper:solo" ], "scripts": { "postdeploy": "node server/models/database.js" }, "env": { "TZ": "America/Los_Angeles" } }
正如你所看到的, postdeploy
脚本应该调用database.js
脚本,如下所示:
const pg = require('pg'); const connectionString = process.env.DATABASE_URL; const client = new pg.Client(connectionString); client.connect(); client.query('CREATE TABLE IF NOT EXISTS table_name (id uuid, url VARCHAR(2000), \ title TEXT, description TEXT, been_visited BOOLEAN DEFAULT false, created_at TIMESTAMP DEFAULT NOW())', (err, res) => { if (err) { client.end(); return console.error('error with PostgreSQL database', err); } }); client.end();
我知道查询工作,因为我在本地testing它,但是当我用上面的app.json
testingbutton,我仍然得到错误error: relation "tanabata_tree" does not exist
– 意味着表从未创build。
我在哪里做错了什么?
1 : https : //devcenter.heroku.com/articles/heroku-button https://devcenter.heroku.com/articles/heroku-button
2 : https : //devcenter.heroku.com/articles/app-json-schema