简体   繁体   English

使用 heroku (ruby on rails) 从 postgres 数据库中删除数据

[英]delete data from postgres database using heroku (ruby on rails)

I am developing an website with heroku (ruby on rails).我正在使用heroku(ruby on rails)开发一个网站。 I use the postgres database.我使用 postgres 数据库。 Now I made a mistake and there is a row inside this database which I want to delete.现在我犯了一个错误,这个数据库中有一行我想删除。 But I have no access.但我无权访问。 The data is shown on the website.数据显示在网站上。 But there is no picture or other content.但是没有图片或其他内容。 It was just a trial and a big error ;)这只是一次试验和一个大错误;)

How can I delete this data without to delete the whole database?如何在不删除整个数据库的情况下删除这些数据? I found nothing...我什么都没发现...

I hope you can help me.我希望你能帮助我。

with best regards致以最好的问候

Assuming your rails application is otherwise configured properly (I'm guessing it is, since you got it far enough to insert that incorrect row in the database), as the comments said, you can use the heroku toolbelt to access the rails console.假设您的 rails 应用程序以其他方式正确配置(我猜是这样,因为您已经足够在数据库中插入不正确的行),正如评论所说,您可以使用 heroku 工具带访问 rails 控制台。

To install the heroku toolbelt follow the instructions here .要安装 heroku 工具带,请按照此处的说明进行操作。

Since you've been developing in Rails, this will probably feel most familiar.由于您一直在使用 Rails 进行开发,因此您可能会对此感到最熟悉。

Open a terminal window, change to your application directory, and run打开终端窗口,切换到您的应用程序目录,然后运行

heroku run rails c

This should give you something like this这应该给你这样的东西

Loading production environment (Rails 4.2.0)
irb(main):001:0>

From there you can type rails code and the console will run that code against your database.从那里您可以键入 rails 代码,控制台将针对您的数据库运行该代码。

eg if the model you want to delete is a Post, then you might type例如,如果您要删除的模型是一个帖子,那么您可以输入

Post.pluck :name

to find the names of the posts you have.查找您拥有的帖子的名称。

Then you might do那么你可能会这样做

Post.where(name: 'my broken post').destroy_all

I'm a little late here, but this might help someone who stumbles across this thread...我在这里有点晚了,但这可能会帮助那些偶然发现这个线程的人......

If you go to your Heroku app's dashboard (through the website) > settings > "Reveal Config Vars" > DATABASE_URL, and then paste that URL into the browser.如果您转到 Heroku 应用程序的仪表板(通过网站)> 设置 >“显示配置变量”> DATABASE_URL,然后将该 URL 粘贴到浏览器中。

I use TablePlus for database management, when I paste the link into the browser it asks if it can open TablePlus and then I can edit my production database in real time like I would in development.我使用 TablePlus 进行数据库管理,当我将链接粘贴到浏览器时,它会询问它是否可以打开 TablePlus,然后我可以像在开发中一样实时编辑我的生产数据库。

I'm not sure what pasting the URL into the browser will do if you don't have TablePlus.如果您没有 TablePlus,我不确定将 URL 粘贴到浏览器中会做什么。 I imagine it will request to open any other SQL management app you might have.我想它会要求打开您可能拥有的任何其他 SQL 管理应用程序。

You can delete database and recreate using below command 您可以使用以下命令删除数据库并重新创建

heroku pg:reset database

again create database 再次创建数据库

heroku run rake db:migrate

heroku run rake db:seed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM