Rails error creating database with postgres - ruby-on-rails

the first time when postgresql is installed in my local machine i created a user "user1" using the following command :
$ sudo su postgres -c psql
postgres=# CREATE ROLE user1 SUPERUSER LOGIN;
postgres=# \q
then setup my database.yml
development:
adapter: postgresql
encoding: unicode
database: appname_development
pool: 5
timeout: 5000
username: user1
password:
and all work fine, so today i created a new app then i did the same steps (creating a new user "user2" and modify my database.yml)
development:
adapter: postgresql
encoding: unicode
database: application2_development
host: localhost
pool: 5
timeout: 5000
username: user2
password:
but when i try to create the database
$ rake db:create:all
i get this error :
FATAL: Peer authentication failed for user "application2"
i tried to fix the problem by adding host: localhost to my database.yml but now i get another error which is :
fe_sendauth: no password supplied
can someone explains to me what means this error, and why the first time when i created the first user it doesn't show me this error ? then i will be thankful to know the solution as well.
thank you

I have already faced this problem and fixed it.
I think, It will be helpful to you.
please find 'pg_hba.conf' and postgresql.conf files.
generally, pg_hba.conf and postgresql.conf files located at /etc/postgresql/9.1/main/.
put `local all all md5' in pg_hba.conf and save it.
open postgresql.conf file and if listen_addresses = 'localhost' and port = 5432 line are commented. then uncomment to it.
Finally restart to pg server and try to create database.

Related

ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist when I try to migrate bd in rails

I cloned a friend's git repo and I'm trying to migrate the db. I started postgres, but when I run rails db:migrate, I keep getting the errors:
Rails Error: Unable to access log file.
and
ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist
I've tried all available solutions online but keep getting the same error. Does anyone know what I'm doing wrong?
Try the following
Setting Up Postgres
Create a Postgres user for the Rails app we'll create in the next step. To do this, switch into the Postgres user:
su - postgres
Then create a user (or a "role", as Postgres calls it):
create role myapp with createdb login password 'password1';
and make sure you have config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
The solution for me was:
createuser -s postgres
After I ran that on the command line my rake task to create the DB worked

No password Supplied, but no changes made

I just opened an app I haven't worked on in a while to get back to work. When I tried opening the app in the browser on my local machine, I get this error PG::ConnectionBad (fe_sendauth: no password supplied):. I haven't messed with it lately, so I'm not sure what might have broken. Here's my database.yml (just default rails created verbage)
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
host: localhost
development:
<<: *default
database: project2_development
Any ideas what might have broken? Like I said, I haven't changed anything recently with this app, so I'm not sure what's going on.
The error clearly shows that postgresql the password you have not supplied, and after successful installation of gem 'pg'
perform the following,
sudo -i -u postgres
you will be as a user of postgres eg:- postgres#user >
psql
you will login in postgresql console
alter user user_name with password 'new_password';
will change the password as provided
\q
exit the psql console
change the database.yml as
username: 'postgres
password: 'new_password'
rake db:create
now try to create the database from rails
Should work !

Cannot connect to Postgresql server, locally

I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.
I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.
I edited my Gemfile to
gem 'pg'
for both production and development and did bundle install.
I edited my database.yml file to this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
Like Ryan says to do in the video, I try this command:
rake db:create:all
Only to get this error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql
After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.
So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to still get the same error of not being able to connect.
I've ran the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well.
Just in case it's needed, when I run
which psql
I receive this:
/usr/local/bin/psql
Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?
Lazy option: Add host: localhost to your database.yml.
Only slightly less lazy option: Uninstall and reinstall the pg gem.
What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.
Just open a command prompt and type
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.
Maybe add host: localhost
to you database.yml

Postgresql or psql 'rails db' password error

I am trying to prompt the psql interface to try to create a database, actually following from Dr. Hartl's tutorials http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=3.2.
I created the project with:
rails new postgr_ --database=postgresql
I added passwords to the database.yml file:
development:
adapter: postgresql
encoding: unicode
database: postgr__development
pool: 5
username: postgr_
password: 12345
test:
adapter: postgresql
encoding: unicode
database: postgr__test
pool: 5
username: postgr_
password: 12345
production:
adapter: postgresql
encoding: unicode
database: postgr__production
pool: 5
username: postgr_
password: 12345
I then enter into terminal:
$ rails db
And I get the following error after entering my password:
psql: FATAL: password authentication failed for user "postgr_"
I've been going at this a good part of yesterday and all day today and was unable to work around this. I may very well be missing something fundamental, if you spot it please let me know. Thank you!
You're missing the steps to setup the postgresql role and database creation.
This procedure depends on the system you are using. I will assume that you are using a mainstream linux distribution.
First login to the postgresql account. You may use one of the following commands:
$ su - postgres
or
$ sudo -i -u postgres
Once logged in, start the psql program:
$ psql template1
At the psql prompt, create a new user role and a database for your project:
=> create role postgr_ with createdb login password '12345';
Then simply quit the program
=> \q
And logout from the postgresql user account
$ exit
Then you should be able to run the rail db command successfully

FATAL: database "postgres" does not exist

Weird error. All help appreciated.
Here's my database.yml
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: username
password:
test:
adapter: postgresql
encoding: unicode
database: app_test
pool: 5
username: username
password:
production:
adapter: postgresql
encoding: unicode
database: app_production
pool: 5
username: username
password:
When I create the databases manually and try to use them, for example rake db:test:preare I get the following error:
FATAL: database "postgres" does not exist
When I try to drop the databases I get the following errors:
Couldn't drop app_development : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop app_test : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop app_production : #<PG::Error: FATAL: database "postgres" does not exist
If I try to create the databases through with rake db:create:all I get the following errors:
app_development already exists
app_test already exists
app_production already exists
So it appears that my database.yml is ok. But for some reason it's looking for a database called postgres when that's not what's in my database.yml.
Any help appreciated.
EDIT:
Here is more from the trace of rake:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_production", "pool"=>5, "username"=>"username", "password"=>nil}
[Mac OS X] I've never had any problem with PostGreSQL while using Linux. However, I started working with a MacBook and got the same error as you. So... Probably this is not the solution you were expecting for, but this one app solved all my headaches. I just wanted to share it with you guys.
http://postgresapp.com/
You've not said whether you actually have a postgres database, which is what the error is complaining about.
Presumably not, and this "rake" tool is connecting as user postgres to database postgres so it can create the new databases. Obviously it needs to connect as someone to do anything.
Do take the time to skim through the PostgreSQL manuals and understand how to enable/control logging. It'll serve you well in the long run. I'm guessing there is a debug/log setting for rake/rails too.
I'm using Mac OS X, I had the same problem with python and sqlalchemy.
Just like Flavio suggested, check postgresapp and check How to connect section.
from sqlalchemy import create_engine
engine = create_engine('postgresql://localhost/[YOUR_DATABASE_NAME]')

Resources