You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Cloud9搭建Rails6+PostgreSQL环境时pg安装及数据库连接问题求助

问题背景

我正在尝试在AWS Cloud9中搭建搭配PostgreSQL数据库的Rails 6环境,已执行操作步骤如下:

  • 在Cloud9中创建了运行Ubuntu Server 18.04 LTS系统的新环境
  • 执行gem install rails安装最新版Rails
  • 执行rails -v查看版本,返回结果为Rails 6.1.4.1
  • 执行rails new my_app -d postgresql创建新Rails应用,命令执行报错,完整构建日志附后

已尝试排障操作:

  • 先执行sudo apt-get install libpq-dev,再运行gem install pg,此时报错提示需要安装yarn,执行source <(curl -sL https://cdn.learnenough.com/yarn_install)完成yarn安装
  • 再次运行rails new my_app -d postgresql,命令执行过程中出现了几条webpacker相关警告,以及http-proxy-middleware@2.0.1: The engine "node" is incompatible with this module的报错,但整体看起来运行成功
  • 进入新建的应用目录执行rails db:migrate,得到如下报错:
rails aborted! ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

首次执行rails new my_app -d postgresql的完整报错日志:

create  
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
      create  .gitattributes
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /home/ubuntu/environment/my_app/.git/
      create  package.json
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/stylesheets/application.css
      create  app/channels/application_cable/channel.rb
      create  app/channels/application_cable/connection.rb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/javascript/channels/consumer.js
      create  app/javascript/channels/index.js
      create  app/javascript/packs/application.js
      create  app/jobs/application_job.rb
      create  app/mailers/application_mailer.rb
      create  app/models/application_record.rb
      create  app/views/layouts/application.html.erb
      create  app/views/layouts/mailer.html.erb
      create  app/views/layouts/mailer.text.erb
      create  app/assets/images
      create  app/assets/images/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  bin/spring
      create  bin/yarn
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/cable.yml
      create  config/puma.rb
      create  config/spring.rb
      create  config/storage.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/content_security_policy.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults_6_1.rb
      create  config/initializers/permissions_policy.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/master.key
      append  .gitignore
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp
      create  tmp/.keep
      create  tmp/pids
      create  tmp/pids/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor
      create  vendor/.keep
      create  test/fixtures/files
      create  test/fixtures/files/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/channels/application_cable/connection_test.rb
      create  test/test_helper.rb
      create  test/system
      create  test/system/.keep
      create  test/application_system_test_case.rb
      create  storage
      create  storage/.keep
      create  tmp/storage
      create  tmp/storage/.keep
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_6_1.rb
         run  bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Fetching rake 13.0.6
Installing rake 13.0.6
Using concurrent-ruby 1.1.9
Using i18n 1.8.10
Fetching minitest 5.14.4
Installing minitest 5.14.4
Using tzinfo 2.0.4
Using zeitwerk 2.4.2
Using activesupport 6.1.4.1
Using builder 3.2.4
Using erubi 1.10.0
Fetching mini_portile2 2.6.1
Installing mini_portile2 2.6.1
Using racc 1.5.2
Using nokogiri 1.12.4 (x86_64-linux)
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.12.0
Using rails-html-sanitizer 1.4.2
Using actionview 6.1.4.1
Using rack 2.2.3
Fetching rack-test 1.1.0
Installing rack-test 1.1.0
Using actionpack 6.1.4.1
Using nio4r 2.5.8
Using websocket-extensions 0.1.5
Fetching websocket-driver 0.7.5
Installing websocket-driver 0.7.5 with native extensions
Using actioncable 6.1.4.1
Using globalid 0.5.2
Using activejob 6.1.4.1
Using activemodel 6.1.4.1
Using activerecord 6.1.4.1
Using marcel 1.0.1
Using mini_mime 1.1.1
Using activestorage 6.1.4.1
Using mail 2.7.1
Using actionmailbox 6.1.4.1
Using actionmailer 6.1.4.1
Using actiontext 6.1.4.1
Fetching public_suffix 4.0.6
Installing public_suffix 4.0.6
Fetching addressable 2.8.0
Installing addressable 2.8.0
Fetching bindex 0.8.1
Installing bindex 0.8.1 with native extensions
Fetching msgpack 1.4.2
Installing msgpack 1.4.2 with native extensions
Fetching bootsnap 1.8.1
Installing bootsnap 1.8.1 with native extensions
Using bundler 1.17.2
Fetching byebug 11.1.3
Installing byebug 11.1.3 with native extensions
Fetching regexp_parser 2.1.1
Installing regexp_parser 2.1.1
Fetching xpath 3.2.0
Installing xpath 3.2.0
Fetching capybara 3.35.3
Installing capybara 3.35.3
Fetching childprocess 3.0.0
Installing childprocess 3.0.0
Fetching ffi 1.15.4
Installing ffi 1.15.4 with native extensions
Fetching jbuilder 2.11.2
Installing jbuilder 2.11.2
Fetching rb-fsevent 0.11.0
Installing rb-fsevent 0.11.0
Fetching rb-inotify 0.10.1
Installing rb-inotify 0.10.1
Fetching listen 3.7.0
Installing listen 3.7.0
Using method_source 1.0.0
Fetching pg 1.2.3
Installing pg 1.2.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/pg-1.2.3/ext
/home/ubuntu/.rvm/rubies/ruby-2.6.3/bin/ruby -I /home/ubuntu/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0 -r ./siteconf20210910-9968-sc88vh.rb
extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/home/ubuntu/.rvm/rubies/ruby-2.6.3/bin/$(RUBY_BASE_NAME)
        --with-pg
        --without-pg
        --enable-windows-cross
        --disable-windows-cross
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/ubuntu/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-1.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/pg-1.2.3 for inspection.
Results logged to /home/ubuntu/.rvm/gems/ruby-2.6.3/extensions/x86_64-linux/2.6.0/pg-1.2.3/gem_make.out

An error occurred while installing pg (1.2.3), and Bundler cannot continue.
Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  pg
         run  bundle binstubs bundler
Could not find gem 'pg (~> 1.1)' in any of the gem sources listed in your Gemfile.
       rails  webpacker:install
Could not find gem 'pg (~> 1.1)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

解决方案

按以下步骤依次处理即可完成环境搭建:

1. 安装完整PostgreSQL服务及依赖

之前仅安装了pg gem的编译依赖libpq-dev,未安装PostgreSQL服务端本身,这是数据库连接报错的核心原因:

sudo apt update
sudo apt install -y postgresql postgresql-contrib libpq-dev

安装完成后启动服务并设置开机自启:

sudo service postgresql start
sudo systemctl enable postgresql

2. 升级Node版本解决webpacker依赖报错

Cloud9 Ubuntu18.04默认的Node版本过低,不符合Rails 6 webpacker的依赖要求:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install -g yarn

执行node -v确认返回v16.x版本号即可。

3. 配置PostgreSQL用户权限

默认PostgreSQL仅创建了postgres超级用户,需要给Cloud9默认的ubuntu用户授予数据库操作权限:

sudo -u postgres createuser -s ubuntu

4. 重新创建Rails项目并初始化数据库

先删除之前生成的不完整项目文件夹,再重新创建:

rm -rf my_app
gem install pg
rails new my_app -d postgresql
cd my_app
rails db:create
rails db:migrate

执行完成后没有报错即代表环境搭建完成,可运行rails s启动服务测试。


内容的提问来源于stack exchange,提问作者David Nelson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 03:51:03