How to create new postgres user for a new Rails project? - ruby-on-rails

I created a new rails project rails new devise_gem_app -d postgresql and changed config/database.yml to these:
default: &default
adapter: postgresql
encoding: unicode
username: devise_gem_app
password: devise_gem_app
host: localhost
development:
<<: *default
database: devise_gem_app_development
test:
<<: *default
database: devise_gem_app_test
But I don't know how to connect the user to this batabase
I create a user like this: createuser devise_gem_app -W set it as a super user and I set password to: devise_gem_app.
But when I try rake db:migrate I get:
PG::ConnectionBad: FATAL: password authentication failed for user "devise_gem_app"
FATAL: password authentication failed for user "devise_gem_app"
I even chenged last line of pg_hbf.conf to:
#local replication postgres trust
... and nothing ...
I know this should be easy but I just don't see what am I doing wrong.

I'm not sure what the createuser command is doing, but you could always set the password for the devise_gem_app user directly in postgres.
ALTER user devise_gem_app WITH password 'devise_gem_app';

create user user_name with password 'password' createdb;
And in pg_hbf.conf file
#Database administrative login by Unix domain socket
local all postgres peer

In the end I ended following this tutorial: http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
But I am upset that it seems rails don`t support, creating new users for new projects db, which seems to be common practice.

Related

Rails migration with a different user than regular connections

How can I connect to the DB with a different user when running rails migrations?
The use case is to create a different Postgres user for migrations than that used to run the webserver. Whereby the regular webserver DB user is limited to CRUD for data, but not have grants or abilities like DROP etc.
In order for that to happen, I need Rails migrations to run with a different user with full Create and Drop table access. (Creds could live in the Rails encrypted credentials file, but storing them in ENV on server is probably fine).
So, to be clear, setting up the users is not a problem, I can do all that. I just want to know if there is a best-practice way to change the user/creds used by rails db:migrate
You can create a new environment which has its own database user configured in config/database.yml. Example:
default: &default
adapter: postgresql
database: dev_db_name
username: dev_user_name
password: dev_password
development:
<<: *default
production:
<<: *default
database: production_db_name
username: production_user_name
password: production_user_password
migrations:
<<: *default
database: production_db_name
username: migration_user_name
password: migration_user_password
So when you run migrations you have to specify the new environment:
RAILS_ENV=migrations rake db:migrate
IMPORTANT: When create a new environment be sure that you complete this required steps:
Create a new config/environments/YOUR_ENVIRONMENT.rb file
Create a new database configuration entry in config/database.yml if your application uses database
Create a new secret key base entry in config/secrets.yml for apps on Rails 4.1 and higher
More information click here
You can set the database connection through ENV["DATABASE_URL"]. This takes precedence over and is merged with over any settings in config/database.yml:
DATABASE_URL=postgresql://localhost/some_other_database?username=max&password=p4ssw0rd rails db:migrate
If you want to do anything more advanced like using the encrypted credentials to store the password I would recommend writing a custom rake task.

Mysql2::Error Access denied for user

I have some rails applications on my mac and they work just fine.
I have just installed a new one that uses mysql2 gem, I am starting
the server but when I am visiting the application I am getting an
Access denied error.
I am trying to follow this solution
Mysql2::Error: Access denied for user 'test'#'localhost' to database 'depot_test'
I am logging in as a root user but when I am trying to create a new database I am getting an access denied error.
Changes in your config / database.yml the user name root. test user does not exist in MySQL. I think in your development environment should look like:
Example
development:
adapter: mysql2
database: depot_test
pool: 5
host: localhost
encoding: utf8
username: test
password:
Change username to root or create test user with privileges. Remember that Rails uses environments(test, development, production u other). In your case is depot_development or another name that is not equal to another environment.
I hope it helps you.

Testing a Rails Application with Multiple Databases

I have a rails application which uses two databases in production environment, Users and Process. The Users model uses this customized ActiveRecord class:
class UserActiveRecord < ActiveRecord::Base
establish_connection "#{Rails.env}_users_db"
end
class User < UserActiveRecord
...
Notice that the connection to a specific database is established depending on the environment. To simplify things, in the testing environment I have a single database with all the tables from the two production databases, and my database.yml file looks something like this:
test:
adapter: postgresql
database: db_test
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
test_users_db:
adapter: postgresql
database: db_test <--- Notice that this is the same database only in test env
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
The application runs fine in production, but when I run any test that refers to the User class, the test blocks at the exact point where User is used and nothing happens, it doesn't show any error, it doesn't exit, it just keeps waiting.
I double-checked and the table USERS exists in the test database, in fact if I delete it manually I get the error that the table doesn't exists and when I create it again I get the same behavior reported in the previous paragraph.
I don't have a clue why this is happening, any idea on how can I solve this issue? or how can I debug it so that I can get to the root of the problem? If it helps, I'm using Ruby 1.9.3, Ruby on Rails 3.2.19 and PostgreSQL 9.3.5; any additional details will be posted as required.
Have you tried explicitly stating that it's the same database? Try editing your database.yml file to look like this:
test: &default_test
adapter: postgresql
database: db_test
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
test_users_db:
<<: *default_test

Database not visible

I am trying to switch database from SqLite3 to Postgresql. My site connects to database in db/development but I can't see that file there. This is my database.yml:
development:
adapter: postgresql
database: db/development
pool: 5
timeout: 5000
username: user
When I create new database with createdb -O rolename dbname I don't see that database anywhere. I downloaded Navicat to browse my database but I can't find it.
I created a new migration after I configured app to use postgresql and it successfully migrated to that database. The problem is I can't see it anywhere. How can I insert data to database or open in in GUI if I don't see it anywhere?
When I run psql -U user db/development nothing happens.
When you're using PostgreSQL, the database: should be a simple database name, not something that looks like a filename. You should probably have something more like this:
development:
adapter: postgresql
database: appname_development
pool: 5
timeout: 5000
username: user
where appname is some sort of name for your application.
PostgreSQL doesn't use a simple file in your Rails directory tree to hold the database, PostgreSQL (and MySQL and SQL Server and ...) will use a collection of files off somewhere in its own directory and you generally shouldn't worry about where they are or what their structure is. If you want to dump the database for backup purposes then you'll want to use pg_dump.
You must have to provide host and port too.
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Default port of postgres will be 5432.

rake db:create:all => FATAL: password authentication failed for user "foo"

Trying to setup a postgresql db for the first time. I'm using this railscast. After many hours of frustrating issues, I got as far as rake db:create:all. Now I get the error:
password authentication failed for user "pgtest"
Here is my pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
Here is my database.yml
development:
adapter: postgresql
encoding: unicode
database: pgtest_development
pool: 5
username: pgtest
password: password
test:
adapter: postgresql
encoding: unicode
database: pgtest_test
pool: 5
username: pgtest
password: password
I tried it first with no password and it gives the error:
fe_sendauth: no password supplied
What am I doing wrong? Thanks :)
if you just wrote changes to your pg_hba.conf and didn't RELOAD or RESTART PostgreSQL do that first. Those are common causes of what you describe.
If that didn't fix it, then you are either editing the wrong pg_hba.conf or you have rules specified earlier in the file that you aren't showing us. Rule out those cases in that order.

Resources