【问题标题】:psycopg2 on elastic beanstalk - can't deploy app弹性豆茎上的 psycopg2 - 无法部署应用程序
【发布时间】:2016-02-18 06:49:43
【问题描述】:

我正在将我的第一个应用程序(Flask)部署到 EB,但遇到了问题。使用 PostgreSQL 需要我安装 psycopg2,但我不知道如何让它运行。

我在这篇文章(Psycopg2 on Amazon Elastic Beanstalk)和这篇文章(https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/)中看到我需要添加一个文件名.elasticbeanstalk/01_packages.config,代码如下:

packages:
  yum:
    postgresql-devel: []

我尝试了postgresql93-devel: [] 的不同变体,但仍然没有运气。下面附上我的错误日志。

[2015-11-16T21:03:24.749Z] INFO  [2680]  - [Application update/AppDeployStage0/AppDeployPreHook/03deploy.py] : Starting activity...
[2015-11-16T21:03:27.024Z] INFO  [2680]  - [Application update/AppDeployStage0/AppDeployPreHook/03deploy.py] : Activity execution failed, because: Collecting psycopg2==2.6.1 (from -r /opt/python/ondeck/app/requirements.txt (line 1))
    Using cached psycopg2-2.6.1.tar.gz
      Complete output from command python setup.py egg_info:
      running egg_info
      creating pip-egg-info/psycopg2.egg-info
      writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
      writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
      writing pip-egg-info/psycopg2.egg-info/PKG-INFO
      writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
      warning: manifest_maker: standard file '-c' not found

      Error: pg_config executable not found.

      Please add the directory containing pg_config to the PATH
      or specify the full executable path with the option:

          python setup.py build_ext --pg-config /path/to/pg_config build ...

      or with the pg_config option in 'setup.cfg'.

      ----------------------------------------
  Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-z20gvx7_/psycopg2
  2015-11-16 21:03:27,010 ERROR    Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1
  Traceback (most recent call last):
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
      install_dependencies()
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
      check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
    File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
      raise CalledProcessError(retcode, cmd)
  CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1 (ElasticBeanstalk::ExternalInvocationError)
caused by: Collecting psycopg2==2.6.1 (from -r /opt/python/ondeck/app/requirements.txt (line 1))
    Using cached psycopg2-2.6.1.tar.gz
      Complete output from command python setup.py egg_info:
      running egg_info
      creating pip-egg-info/psycopg2.egg-info
      writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
      writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
      writing pip-egg-info/psycopg2.egg-info/PKG-INFO
      writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
      warning: manifest_maker: standard file '-c' not found

      Error: pg_config executable not found.

      Please add the directory containing pg_config to the PATH
      or specify the full executable path with the option:

          python setup.py build_ext --pg-config /path/to/pg_config build ...

      or with the pg_config option in 'setup.cfg'.

      ----------------------------------------
  Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-z20gvx7_/psycopg2
  2015-11-16 21:03:27,010 ERROR    Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1
  Traceback (most recent call last):
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
      install_dependencies()
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
      check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
    File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
      raise CalledProcessError(retcode, cmd)
  CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 1 (Executor::NonZeroExitStatus)

似乎很清楚我需要安装 psycopg2,但我不确定我可以采取哪些其他步骤来完成这项工作。

【问题讨论】:

  • 我也有。如果我解决了,我会告诉你的。

标签: python postgresql amazon-web-services amazon-ec2 amazon-elastic-beanstalk


【解决方案1】:

很有趣,Amazon Linux 2 的解决方案对我来说最终是最初的问题 - 添加 01_package.conf 的内容为

packages:
  yum:
    postgresql-devel: []

此外,这与我在网上找到的几乎所有使用postgresql96-devel 或其他数字的答案都相反。我发现我需要将包名称设为postgresql-devel SSH 到 EC2 实例并查看所有可用的 Yum 包。

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ yum list available | grep 'postgresql'
freeradius-postgresql.x86_64           3.0.13-10.amzn2                amzn2-core
pcp-pmda-postgresql.x86_64             3.12.2-5.amzn2                 amzn2-core
postgresql.x86_64                      10.4-5.amzn2.0.2               amzn2extra-postgresql10
postgresql-contrib.x86_64              10.4-5.amzn2.0.2               amzn2extra-postgresql10
postgresql-devel.x86_64                10.4-5.amzn2.0.2               @amzn2extra-postgresql10
postgresql-docs.x86_64                 10.4-5.amzn2.0.2               amzn2extra-postgresql10
...

我看到它只是postgres-devel。一旦我将其更改为它部署就可以了。如果您在运行eb deploy 之前将eb 与git 一起使用,请确保您提交您的更改 - 或者至少将更改添加到您的git 暂存区域并同时使用eb deploy --staged 运行。那个让我四处奔波,以为我已经尝试过包名,而我还没有?

我发现answers on this stackoverflow post here 也很有帮助。祝你好运!


我最终需要一个更新的 psql 客户端 (11.x),而不是使用上述答案的默认值 (9.2 在发布时)。 This answer here 是我让它工作的唯一方法。答案粘贴在这里以供后代使用...

packages:
    yum:
        amazon-linux-extras: []

commands:
    01_postgres_activate:
        command: sudo amazon-linux-extras enable postgresql11
    02_postgres_install:
        command: sudo yum install -y postgresql-devel

【讨论】:

  • 你的评论为我解决了这个问题,你太棒了。对于其他使用 Amazon Linux 2 的人来说,这就是解决方案。
  • 谢谢 ? 这个给我带来了很多麻烦 - 特别是因为大多数文档都说要使用 postgresql96-devel(包括 AWS 的)或其他版本......甚至在没有运气的情况下提供赏金。很高兴我的努力帮助了别人!!
  • 感谢您的回答!花了很长时间才弄清楚版本号弄乱了我的部署,特别是当这么多教程指向带有版本的 postgresql 时
  • postgresql11 末尾的 11 是多少?如果我尝试使用 postgres 12.5 连接到 RDS,我应该使用 12 吗?
  • 我认为你可能不走运,除非你想自己构建它。如果您与支持人员交谈,请发表评论。我用amazon-linux-extras list | grep 'postgres' 检查了 AL2,只看到 9.6、10 和 11。我记得有一段时间让弹性豆茎开始运转……祝你好运。
【解决方案2】:

即使在安装 postgresql93-devel 之后,我仍然收到错误消息“没有名为 psycopg2 的模块”。为了解决这个问题,我必须使用 eb ssh 连接到我的环境,然后执行 sudo pip install psycopg2 来解决这个问题。

【讨论】:

    【解决方案3】:

    我解决了,你需要先打开你的EC2实例,然后像这样安装

    sudo yum install postgresql-devel
    

    然后部署您的 EB

    【讨论】:

    • 只有在您提供的命令之后才能完成sudo install psycopg2
    • 谢谢。只是想知道,如果我们在使用 EB 时有多个 ec2 实例,我们是否需要在每个实例上运行此命令?
    • 这并不能解决问题。如果您有更多实例,或者当实例被替换时,这将再次失败
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 2021-03-13
    • 2017-11-27
    • 2013-07-06
    • 2016-01-13
    • 2015-04-02
    • 2013-05-25
    • 2016-12-15
    相关资源
    最近更新 更多