postgresql Cloud9 postgres

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26545746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:40:03  来源:igfitidea点击:

Cloud9 postgres

ruby-on-railspostgresqlcloud9-ide

提问by jonny_FIVE

I am trying to set up a postgres database in a Rails app in Cloud9.

我正在尝试在 Cloud9 的 Rails 应用程序中设置 postgres 数据库。

I have followed the instructions here: https://docs.c9.io/setting_up_postgresql.htmland set up a database called cc_database.

我已按照此处的说明操作:https: //docs.c9.io/setting_up_postgresql.html 并设置了一个名为 cc_database 的数据库。

My database.yml file looks like this:

我的 database.yml 文件如下所示:

development:
  adapter: postgresql
  encoding: SQL_ASCII
  database: cc_database
  pool: 5
  username: postgres
  password: password

When I run rake db:setup I get the following error:

当我运行 rake db:setup 时,出现以下错误:

 PG::ConnectionBad: FATAL:  Peer authentication failed for user "postgres"

I am quite new to all this, so any advice would be much appreciated.

我对这一切都很陌生,所以任何建议都将不胜感激。

回答by Tony Nguyen

Do the following steps:

执行以下步骤:

  1. Create a new username and password for postgresql on cloud9:

    $ sudo service postgresql start
    $ sudo sudo -u postgres psql
    postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
    postgres=# \q
    
  2. Create ENV variables on cloud9:

    $ echo "export USERNAME=username" >> ~/.profile
    $ echo "export PASSWORD=password" >> ~/.profile
    $ source ~/.profile
    

    My database.yml for rails 4.2.0 on cloud9:

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      username: <%= ENV['USERNAME'] %>
      password: <%= ENV['PASSWORD'] %>
      host:     <%= ENV['IP'] %>
    
    development:
      <<: *default
      database: sample_app_development
    
    test:
      <<: *default
      database: sample_app_test
    
    production:
      <<: *default
      database: sample_app_production
    
  3. Include the gem pgin Gemfile and install:

    gem 'pg', '~> 0.18.2'

    $ bundle install
    
  4. Update template1postgresql for database.yml on cloud9:

    postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
    postgres=# DROP DATABASE template1;
    postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
    postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
    postgres=# \c template1
    postgres=# VACUUM FREEZE;
    postgres=# \q
    
  5. From command line run:

    bundle exec rake db:create
    
  1. 在 cloud9 上为 postgresql 创建一个新的用户名和密码:

    $ sudo service postgresql start
    $ sudo sudo -u postgres psql
    postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
    postgres=# \q
    
  2. 在 cloud9 上创建 ENV 变量:

    $ echo "export USERNAME=username" >> ~/.profile
    $ echo "export PASSWORD=password" >> ~/.profile
    $ source ~/.profile
    

    我在 cloud9 上运行 rails 4.2.0 的 database.yml:

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      username: <%= ENV['USERNAME'] %>
      password: <%= ENV['PASSWORD'] %>
      host:     <%= ENV['IP'] %>
    
    development:
      <<: *default
      database: sample_app_development
    
    test:
      <<: *default
      database: sample_app_test
    
    production:
      <<: *default
      database: sample_app_production
    
  3. pg在 Gemfile 中包含 gem并安装:

    宝石'pg','~> 0.18.2'

    $ bundle install
    
  4. 在 cloud9 上为 database.yml更新模板 1postgresql:

    postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
    postgres=# DROP DATABASE template1;
    postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
    postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
    postgres=# \c template1
    postgres=# VACUUM FREEZE;
    postgres=# \q
    
  5. 从命令行运行:

    bundle exec rake db:create
    

回答by Carlos Eduardo

The postgresql in cloud9 is setup to authenticate with peer when localhost connection. So the quickly resolution is change the user in your database.yaml to current user. In my case the name of user is ubuntu. To see your current user use the command

cloud9 中的 postgresql 设置为在本地主机连接时与对等方进行身份验证。因此,快速解决方案是将 database.yaml 中的用户更改为当前用户。在我的例子中,用户名是 ubuntu。要查看您当前的用户,请使用命令

$ echo $USER

$ echo $USER

So a list of command in terminal is .

所以终端中的命令列表是 .

$ sudo su - postgres
$ createuser ubuntu -dslP
$ Enter password for new role: **same password from your yaml file**
$ Enter it again:

Set your yaml file like this

像这样设置你的 yaml 文件

development:
  adapter: postgresql
  encoding: SQL_ASCII
  database: cc_database
  pool: 5
  username: ubuntu
  password: password

Now you can run

现在你可以运行

rake db:create
rake db:migrate

回答by user1991035

Use the username "ubuntu" with a blank password. My database.yml looks like this:

使用用户名“ubuntu”和空白密码。我的 database.yml 看起来像这样:

development:
adapter: postgresql
encoding: unicode
database: myflix_development
pool: 5
username: ubuntu
password:

开发:
适配器:postgresql
编码:unicode
数据库:myflix_development
池:5
用户名:ubuntu
密码:

test:
adapter: postgresql
encoding: unicode
database: myflix_test
pool: 5
username: ubuntu
password:

测试:
适配器:postgresql
编码:unicode
数据库:myflix_test
池:5
用户名:ubuntu
密码:

If it then complains the password is wrong, try changing the password using Cloud9's instructions:

如果它随后抱怨密码错误,请尝试使用 Cloud9 的说明更改密码:

In the command line, type: sudo sudo -u postgres psql

在命令行中,键入: sudo sudo -u postgres psql

postgres=# \password
Enter new password: leave it blank and press enter
Enter it again: leave it blank and press enter
postgres=# \q

postgres=#\password
输入新密码:leave it blank and press enter
再次输入:leave it blank and press enter
postgres=#\q

I'm pretty new to this to. Hope that works!

我对此很陌生。希望有效!

回答by Eliot Sykes

How to setup PostgreSQL & Rails on Cloud9

如何在 Cloud9 上设置 PostgreSQL 和 Rails

At time of writing, Cloud9 has PostgreSQL pre-installed, so you won't need to install it yourself. However, its not running by default, so you will need to start it with this command in the terminal:

在撰写本文时,Cloud9 已预装 PostgreSQL,因此您无需自行安装。但是,默认情况下它不运行,因此您需要在终端中使用以下命令启动它:

sudo service postgresql start

Change the PostgreSQL password to 'password' (or choose a different password):

将 PostgreSQL 密码更改为“password”(或选择其他密码):

sudo sudo -u postgres psql

# This will open the psql client.

# Type \password and press enter to begin process
# of changing the password:
postgres=# \password

# Type your new password (e.g. "password") and press enter twice:
Enter new password: 
Enter it again: 

# Password changed, quit psql with \q
postgres=# \q 

Edit your config/database.ymlto be:

编辑你config/database.yml是:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

  # Important configs for cloud9, change password value
  # to what you entered in the previous psql step.
  template: template0
  username: ubuntu
  password: password

development:
  <<: *default
  database: your_app_name_development

test:
  <<: *default
  database: your_app_name_test

production:
  <<: *default
  database: your_app_name_production
  username: your_app_name
  password: <%= ENV['YOUR_APP_NAME_DATABASE_PASSWORD'] %>

(Note the template, username, and passwordconfigs in the defaultsection above are essential).

(请注意template,上一节中的username、 和password配置default是必不可少的)。

Add the pggem to your Gemfile:

pggem添加到您的Gemfile:

gem 'pg'

Run bundle install.

运行bundle install

Remove the sqlite gem from your Gemfile(and optionally delete the db/*.sqlite3files).

从您的Gemfile(并可选择删除db/*.sqlite3文件)中删除sqlite gem 。

Create the database, load schema.rb, and seed the database using the db:setuptask:

创建数据库,加载 schema.rb,并使用以下db:setup任务为数据库设定种子:

bundle exec rake db:setup

# Run bin/rake -AD db to see all db-related tasks

Start or restart your rails app and check it is working.

启动或重新启动 Rails 应用程序并检查它是否正常工作。

Note, the non-seed data from your old sqlite database will not be present in the new database.

请注意,来自旧 sqlite 数据库的非种子数据将不会出现在新数据库中。

If you ever want to use the psql client to interact with PostgreSQL directly, in the terminal run psqlor run bin/rails db.

如果您想使用 psql 客户端直接与 PostgreSQL 交互,请在终端运行psql或运行bin/rails db.

回答by TinkerTenorSoftwareGuy

For me, doing the steps in Cloud9 workspace setup with Rails and Postgresqlwasn't enough. It was passing in my user, but not the password. echo $USERNAMEwas coming up with nothing.

对我来说,使用 Rails 和 Postgresql执行Cloud9 工作区设置中的步骤是不够的。它传递了我的用户,但没有传递密码。 echo $USERNAME一无所获。

Solution

解决方案

$ sudo su - postgres
$ createuser ubuntu -dslP

Then I did this:

然后我这样做了:

sudo sudo -u postgres psql
postgres=# \password
Enter new password: entered a real password
Enter it again: entered it again
postgres=# \q

Then I did this in my yaml file (note I killed the host part):

然后我在我的 yaml 文件中做了这个(注意我杀死了主机部分):

development:
  adapter: postgresql
  encoding: unicode
  database: my_database_name
  pool: 5
  username: ubuntu
  password: actual_password

Then I was able to create my database with:

然后我能够使用以下命令创建我的数据库:

rake db:create

And my Rails server started without any more hiccups.

我的 Rails 服务器启动时没有任何问题。

回答by jonny_FIVE

Found the solution. Needed to edit the pg_hba.conf file to change the authentication from peer to md5 like this:

找到了解决办法。需要编辑 pg_hba.conf 文件以将身份验证从 peer 更改为 md5,如下所示:

local    postgres     postgres     md5

It's hard to find the file as it can only be accessed via the terminal in cloud9. You cannot find it in the file tree.

很难找到该文件,因为它只能通过 cloud9 中的终端访问。您无法在文件树中找到它。

If you type the following into postgres it will show you the location

如果你在 postgres 中输入以下内容,它会显示你的位置

SHOW hba_file;

You can then find and edit in in vim via the terminal.

然后,您可以通过终端在 vim 中查找和编辑。

回答by foureyedraven

A short version when you have rails app prior to creating any databases:

在创建任何数据库之前拥有 rails 应用程序时的简短版本:

add the gem 'pg' remove gem sqlite3

添加 gem 'pg' 删除 gem sqlite3

$ bundle install
$ gem install pg

Then,

然后,

$sudo service postgresql start
$psql -c "create database myapp_development owner=ubuntu"

as per https://community.c9.io/t/how-do-i-set-up-postgresql-on-c9-for-my-rails-app/2614/4

根据https://community.c9.io/t/how-do-i-set-up-postgresql-on-c9-for-my-rails-app/2614/4

Changing myapp for whatever name. Then, copy-paste below, changing myapp for whatever name.

为任何名称更改 myapp。然后,在下面复制粘贴,将 myapp 更改为任何名称。

/myapp/config/database.yml
    development:
      adapter: postgresql
      encoding: unicode
      database: myapp_development
      pool: 5
      username: ubuntu
      password:
      timeout: 5000

As above comments, no password needed, username can stay ubuntu.

如上评论,不需要密码,用户名可以留在ubuntu。

$rake db:migrate

If you're using heroku, you won't need the production section in database.yml

如果您使用 heroku,则不需要 database.yml 中的生产部分