PG :: ConnectionBad FATAL:角色“Myname”不存在

我正在尝试使用PostgreSQL,以便我可以部署到Heroku。 但是我不能再运行localhost了么? 我收到以下消息:

PG::ConnectionBad FATAL: role "Myname" does not exist 

这是我的databse.yml

 development: adapter: postgresql database: my_database_development pool: 5 timeout: 5000 test: adapter: postgresql database: my_database_test pool: 5 timeout: 5000 production: adapter: postgresql database: my_database_production pool: 5 timeout: 5000 

这是我的gemfile:

 source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.3' # Use pg as the database for Active Record gem 'pg' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 1.2' group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.1.2' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano', group: :development # Use debugger # gem 'debugger', group: [:development, :test] gem 'rails_12factor', group: :production 

似乎pg需要我创建用户或数据库但是我无法或不知道如何。 找不到任何对我有用的命令(我在windows btw上)

我能做什么?

错误是“角色”Myname“不存在”,

为Postgresql创建用户“Myname”

sudo -u postgres createuser –superuser Myname

它会解决这个问题。

对我createuser -P -d -e Myname是: createuser -P -d -e Myname

 -P If given, createuser will issue a prompt for the password of the new user. This is not necessary if you do not plan on using password authentication. -d The new user will be allowed to create databases. -e Echo the commands that createuser generates and sends to the server. 

如果在OSX上安装带有自制程序的Postgresql,则没有默认的postgres用户,如果没有先设置用户,您将无法直接使用psql

@ user3408293

  1. 安装后为postgresql创建一个用户

    sudo -u postgres createuser –superuser $ USER

    sudo -u postgres createuser pgs_root

  2. 设置postgresql用户的用户密码

    sudo -u postgres psql postgres

    (对于psql提示)postgres =#\ passsword for ex.- postgres =#\ passsword pgs_root

注意您还应该在database.yml文件中的不同环境中添加用户名和密码。

你也可以参考这个链接: Rails:安装pg gem时出错

您应该为Postgresql创建usernamepassword

尝试在psql使用密码创建用户

 CREATE USER Myname WITH PASSWORD 'your_password'; 

你应该把它们添加到你的database.yml

 username: Myname password: your_password 

在Windows上,我相信它更容易一些。

为您的系统安装postgresql和PGAdmin。 看到这个

创建一个名为postgres的用户并为其提供密码。 你会很好地提示你这样做。

然后,当您要创建数据库时 ,只需右键单击您的连接并选择“新建数据库”。 这些数据库的名称应与database.yml中的内容相对应

运行rake db:migrate RAILS_ENV=development (development | test | production)。

这些步骤对我有用。

我不得不进入我的PG管理仪表板并在那里创建一个db / user。 不幸的是,它位于与在线教程不同的子目录中(可能更新了目录目标上次更新)。 幸运的是,我能够找到它并在那里创建表/用户,更新我的database.yml文件然后我的应用程序能够工作!