简体   繁体   English

如何使用Postgres在Heroku上安装应用

[英]How to install app on heroku with postgres

I am trying to install an app in Heroku cloud. 我正在尝试在Heroku云中安装应用程序。 This python based app is built on postgresql. 这个基于python的应用程序基于postgresql构建。 I have set up this app in my local machine(Ubuntu). 我已经在本地计算机(Ubuntu)中设置了此应用。 I have following queries. 我有以下查询。

  1. How to install postgresql for this app on heroku? 如何在heroku上为此应用安装postgresql?
  2. Do I have to make same set up again on Heroku as I did on local machine? 我是否必须像在本地计算机上一样在Heroku上再次进行相同的设置? Like setting up of Flask etc.? 喜欢设置Flask等吗?
  3. Can I submit code in many stages? 我可以分多个阶段提交代码吗? eg I have pushed code, after some time modified same code and pushed again. 例如我推送了代码,一段时间后修改了相同的代码并再次推送了。

I have already gone through following url https://devcenter.heroku.com/articles/git#deploying-code but didn't got any satisfactory answer. 我已经通过以下网址https://devcenter.heroku.com/articles/git#deploying-code,但是没有得到满意的答复。

Thanks Sanjeev Yadav 感谢Sanjeev Yadav

Assuming you have heroku belt installed on your computer, create git repository inside your project(which you probably already have) then create Heroku app: 假设您在计算机上安装了heroku皮带,请在您的项目(您可能已经拥有)中创建git存储库,然后创建Heroku应用程序:

heroku create --stack cedar <name_of_your_app>

First add Postgresql to your project: 首先将Postgresql添加到您的项目中:

heroku addons:add heroku-postgresql

after that check if it has been added succesfully: 之后,检查是否已成功添加:

heroku pg:info

This should return something like: 这应该返回类似:

HEROKU_POSTGRESQL_WHITE_URL <== Database name
Plan:        Dev
Status:      available

Then promote the database so heroku knows which one you will be using (you can have several databases in one project). 然后升级数据库,以便heroku知道您将使用哪个数据库(一个项目中可以有多个数据库)。

heroku pg:promote HEROKU_POSTGRESQL_WHITE_URL

At this point if you want to configure your database connections you can either get all the connection info from Heroku page of your project or from environment variable DATABASE_URL which was created when you promoted the database. 此时,如果要配置数据库连接,则可以从项目的Heroku页面或从升级数据库时创建的环境变量DATABASE_URL获取所有连接信息。 If you use the latter you don't have to provide any info yourself, the function that i use frequently is something in this lines: 如果您使用后者,则不必自己提供任何信息,我经常使用的功能如下所示:

if "DATABASE_URL" in os.environ:
        urlparse.uses_netloc.append("postgres")
        url = urlparse.urlparse(os.environ.get("DATABASE_URL"))
        DATABASE = {
             "database" : url.path[1:],
             "user" : url.username,
             "password": url.password,
             "host": url.hostname,
             "port": url.port
               }
        return PostgresqlDatabase(**DATABASE)

This is for peewee ORM , but you can adapt it to any other ORM like SQLAlchemy, just take what's in DATABASE and plug it in to whatever you want. 这是针对peewee ORM的,但是您可以使其适应任何其他ORM(例如SQLAlchemy),只需获取DATABASE并将其插入所需的任何内容即可。

A far as setting up your application, you need to write requirements.txt file with all dependencies that your app uses, this will automatically be parsed and all libraries installed when you first push your project to Heroku, also you need Procfile file inside your project, this tells Heroku which processes to run at which stage, the most important part is web part for instance: 就设置应用程序而言,您需要编写具有应用程序使用的所有依赖项的requirements.txt文件,当您将项目首次推送到Heroku时,该文件将被自动解析并安装所有库,而且您的项目中还需要Procfile文件,这告诉Heroku在哪个阶段运行哪些进程,例如,最重要的部分是web部分:

web: gunicorn main:app

will start gunicorn server serving your site. 将启动为您的网站提供服务的gunicorn服务器。 If you need to run any scripts, for instance script that creates the tables in your database simply type: 如果您需要运行任何脚本,例如在数据库中创建表的脚本,只需键入:

heroku run create_tables.py

from within your repository. 从您的存储库中。

As far as last question goes - yes you can push your code in many stages, your app will be restarted and all new code should work same as on your local machine. 就最后一个问题而言-是的,您可以分多个阶段推送代码,您的应用将重新启动,所有新代码应与本地计算机上的工作方式相同。 Hope this helps. 希望这可以帮助。

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

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