简体   繁体   中英

Unable to connect to local PostgreSQL database

I created a simple rails application:

1

$ rails new myapp1 -d postrgresql

2 . Removed /public/index.html

3 . Created the Homecontroller and the actions

4 . Wrote the routes

myapp1::Application.routes.draw do

  root :to => "home#index" match 'about' => 'home#about' match 'contacts' => 'home#contacts' match 'projects' => 'home#projects' end 

5 . Created the views and the default layout

6 . The bottom line. Here is the database.yml

development:

  adapter: postgresql encoding: unicode database: myapp1_db host: localhost pool: 5 username: myapp1_user password: 1234 test: adapter: postgresql encoding: unicode database: myapp1_db host: localhost pool: 5 username: myapp1_user password: 1234 production: adapter: postgresql encoding: unicode database: myapp1_db host: localhost pool: 5 username: myapp1_user password: 1234 

and of course I created user and database

sudo -u postgres psql
postgres=# \password
postgres=# create user myapp1_user with password '1234';
postgres=# create database myapp1_db owner myapp1_user; 

But there are some worries:

1 . Now I don't use any database, but in spite of this the application gives an error

PG::Error FATAL: Peer authentication failed for user "myapp1"

2 . It seems like rails turns a blind eye to database.yml . Why does it use the user myapp1 instead of myapp1_user ?

Whitespace is significant in YAML.

production:
  adapter: postgresql
    encoding: unicode
    ...
    password: 1234

Should be:

production:
  adapter: postgresql
  encoding: unicode
  ...
  password: 1234

Provide host and port in the database.yml file, like this:

    development:
      adapter: postgresql
      encoding: unicode
      database: myapp1_db
      host: localhost
      pool: 5
      username: myapp1_user
      password: 1234
      host: localhost
      port: 5432

Works like a charm on Ubuntu 12.04 LTS with Rails 3.2.x and PostgreSQL 9.1.x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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