Why "host:localhost" must be deleted from database.yml under Cent OS 6, PostgreSQL 9.4 and Rails 3.2, or get a error: Ident authentication failed? - ruby-on-rails

All config files described here are the same as my Mac OS's and all works fine in Mac OS.
I got the same error in CentOS 6 x86_64:
Ident authentication failed for user 'abelard'
When running the following two commands:
1. rake db:create
2. psql -d testforabelard2 -U abelard -h localhost
I got the same error after trying these answers 1 and 2.
My /var/lib/pgsql/9.4/pg_hba.con's content is as follows:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
And there is a blank file /var/lib/pgsql/9.4/pg_ident.con
My database.yml's content is as follows:
development:
adapter: postgresql
encoding: unicode
database: social_stream_development
pool: 5
username: abelard
password: password
host: localhost
port: 5432
I found a resolution: the error disappear after deleting host:localhost from the above database.yml. But I can not delete host:localhost because there is a sql_host = localhost generated automatically when using think-sphinx for full-text search.
And for offering the same params as my Mac OS's, I altered PostgreSQL's user abelard :
testforabelard2=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------+-----------
abelard | Superuser | {}
: Create role
: Create DB
postgres | Superuser | {}
: Create role
: Create DB
And I can run the command without -h localhost successfully:
psql -d testforabelard2 -U abelard
I don't know what things I miss, what should I do for correct this error? Any advice will be welcome!

I finally resolved myself easily through moving /var/lib/pgsql/9.4/pg_hba.con to /var/lib/pgsql/9.4/data/pg_hba.con.
The reason for this mistake I made is that I referred to my Mac OS's position of the file pg_hba.con.
Of course, I thank this early blog “FATAL: IDENT AUTHENTICATION FAILED”, OR HOW COOL IDEAS GET BAD USAGE SCHEMAS , which reminded me to realise the wrong place of the above file!

Related

Why can't I run migrations on my test database?

I'm trying to run my RoR app in test environment, but I have a problem with migrations on my test database.
My database.yml file looks like this:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: questionnaires-development
test:
<<: *default
database: questionnaires-test
Now I create my databases with that commands:
sudo -u postgres createdb -O karol questionnaires-development
sudo -u postgres createdb -O karol questionnaires-test
Next I run a migration for the development database:
rails db:migrate
I can easily see it works:
== 20180719143415 CreateQuestions: migrating ==================================
-- create_table(:questions)
-> 0.0894s
== 20180719143415 CreateQuestions: migrated (0.0896s) =========================
== 20180722122658 CreateQuestionnaires: migrating =============================
-- create_table(:questionnaires)
-> 0.1050s
== 20180722122658 CreateQuestionnaires: migrated (0.1051s) ====================
Database migrated.
I can also list my tables with Postgres CLI, everything nice:
questionnaires-development=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+-------
public | ar_internal_metadata | table | karol
public | questionnaires | table | karol
public | questions | table | karol
public | schema_migrations | table | karol
(4 rows)
But now I'd like to do the same with my questionnaires-test database. So I run that command:
rails db:migrate RAILS_ENV=test
But results are worrisome:
Database migrated.
Trying to list my tables my worries get confirmed:
questionnaires-test=# \dt
No relations found.
So, what am I doing wrong?

Heroku pg:push gives psql permisison denied error

Whenever I type heroku pg:push to get my local database on heroku. I get the permission denied error. Any idea how I can solve it.
$ heroku pg:push mexico2019 HEROKU_POSTGRESQL_COBALT_URL --app mexican
Error:
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: SQL command failed
pg_dump: Error message from server: ERROR: permission denied for relation actions
pg_dump: The command was: LOCK TABLE public.actions IN ACCESS SHARE MODE
pg_dump: *** aborted because of error
pg_restore: [archiver] input file is too short (read 0, expected 5)
Database.yml
development:
adapter: postgresql
encoding: unicode
host: localhost
username: ram
password: (password)
database: mexico2019
timeout: 5000
production:
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password: (password)
database: meximexi_pro
pool: 5
timeout: 5000
update:
List of relations
Schema | Name | Type | Owner
--------+------------------------------+----------+-------
public | actions | table | ram
public | actions_id_seq | sequence | ram
public | admins | table | ram
I tried
$ sudo su postgres
postgres=# GRANT ALL PRIVILEGES ON DATABASE mexico2019 TO ram;
GRANT
I just noticed when I type the password wrong it says 'psql: FATAL: password authentication failed for user "alain"', shouldn't it say user ram?
I recommend using PG Backups.

Password authentication failing for Postgresql

I'm creating a Rails app and using a Postgresql database with it. I've created a few tables and a user, core, which is the owner of each of the tables.
postgres=# create user core with password 'n7zD5FG5';
CREATE ROLE
postgres=# create database core_apps_prod with owner core;
CREATE DATABASE
postgres=# create database core_apps_dev with owner core;
CREATE DATABASE
postgres=# create database core_apps_test with owner core;
CREATE DATABASE
And my database.yml file:
development:
adapter: postgresql
database: core_apps_dev
username: core
password: n7zD5FG5
host: localhost
However, when I run rake db:migrate, I get the error
rake aborted!
FATAL: password authentication failed for user "core"
I also cannot connect to psql manually: psql -U core -W -d core_apps_dev - I get the same error.
How can I allow core to connect to Postgresql on localhost?
The output of SELECT * FROM pg_roles where rolname='core'; is:
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | oid
---------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+---------------+-----------+-------
core | f | t | f | f | f | t | f | -1 | ******** | | | 16392
You need to add privileges to the role when you create it. To allow logging in and using rake db:create use the following:
create role core with login createdb password 'n7zD5FG5';
To fix your problem, try altering the role to allow login:
alter role core with login;
And if that doesn't work, see if making the login valid forever works:
alter role core valid until 'infinity';
More about roles: http://www.postgresql.org/docs/8.2/static/sql-createrole.html
Edit:
I also had to do this when I installed postgres:
$ psql postgres -c 'CREATE EXTENSION "adminpack";'
The issue turned out to be an issue with the port. The Postgresql Activerecord adapter defaults to port 5432, while the port in my configuration was port 5433. My database.yml now looks like this:
development:
adapter: postgresql
database: core_apps_dev
username: core
password: n7zD5FG5
host: localhost
port: 5433
The newly created user probably is lacking database privileges. There are many different privileges you can give to a user.
Assuming that you have admin account access to psql, go into psql and run
GRANT ALL PRIVILEGES ON DATABASE core_apps_dev to core;
Can you please try
alter role core with password "n7zD5FG5";

Why I can't work with potgreSQL testdatabase, however development postgreSQL is working?

I'm trying to start to working with PostgreSQL and have some troubles.
I create database for development and it is working. I've already created table and added some objects. I followed this post to create databases - http://blog.deliciousrobots.com/2011/12/13/get-postgres-working-on-ubuntu-or-linux-mint/
Here is the code, what I run to create test_database
denmed#denmed:~/projects/internet_shop$ sudo -u postgres createdb -O denys internet_shop_test
denmed#denmed:~/projects/internet_shop$ psql -d internet_shop_test -U denys -WPassword for user denys:
denmed#denmed:~/projects/internet_shop$ psql -d internet_shop_test -U denys -W
Password for user denys:
psql (9.1.7)
Type "help" for help.
internet_shop_test=>
This means I create database and can work with it or NOT ?
Then, it other console(when I was logged in test database in another console) I run command
rake test
and get this errors( I will cut it ):
Errors running test:units! #<ActiveRecord::StatementInvalid: PG::Error: ERROR:
database "internet_shop_test" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
: DROP DATABASE IF EXISTS "internet_shop_test">
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [ruby
-I"lib:test" -I"/home/denmed/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.0.3/lib" "/home
/denmed/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.0.3/lib/rake/rake_test_loader.rb"
"test/functional/**/*_test.rb" ]>
Ok, it tells me that I'm accessing the database. Then I closed logged in test db terminal and run rake test again and get this:
PG::Error: ERROR: permission denied to create database
...
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode",
"database"=>"internet_shop_test", "pool"=>5, "username"=>"denys",
"password"=>"********"}
...
PG::Error: FATAL: database "internet_shop_test" does not exist
Here is my database.yml for development and test databases:
development:
adapter: postgresql
encoding: unicode
database: internet_shop_development
pool: 5
username: denys
password: ********
test:
adapter: postgresql
encoding: unicode
database: internet_shop_test
pool: 5
username: denys
password: ********
What I'm doing wrong ?
User "denys" does not have permission to create databases. The error is pretty clear,
I don't know if you can tell rails to create the database using a different (superuser) account, or just skip the create database step.
Oh - you don't need the "-W" flag on psql either, it'll ask you for a password if it wants one.

SQL Server and Rails trouble

note: this is a repost. This question was previously deleted for undisclosed reasons
Ok, I've been trying to get this to work like all day now and I'm barely any further from when I started.
I'm trying to get Ruby On Rails to connect to SQL Server. I've installed unixODBC and configured it and FreeTDS and installed just about every Ruby gem relating to ODBC that exists.
(This has been updated to show the output of isql with -v)
[earlz#earlzarch myproject]$ tsql -S AVP1 -U sa -P pass
locale is "en_US.UTF-8"
locale charset is "UTF-8"
1> quit
[earlz#earlzarch ~]$ isql -v AVP1 sa pass
[IM002][unixODBC][Driver Manager]Data source name not found, and no default driver specified
[ISQL]ERROR: Could not SQLConnect
[earlz#earlzarch myproject]$ rake db:version
(in /home/earlz/myproject)
rake aborted!
IM002 (0) [unixODBC][Driver Manager]Data source name not found, and no default driver specified
(See full trace by running task with --trace)
so, as you can see, tsql works, but not isql. What is the difference in the two that breaks it?
/etc/odbc.ini
[AVP1]
Description = ODBC connection via FreeTDS
Driver = TDS
Servername = my.server
UID = sa
PWD = pass
port = 1232
Database = mydatabase
/etc/odbcinst.ini
[TDS]
Description = v0.6 with protocol v7.0
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
(and yes, I've made sure that the .so files exist)
the relevant part in freetds.conf
[AVP1]
host = my.server
port = 1232
tds version = 8.0
and finally, my database.yml
development:
adapter: sqlserver
mode: odbc
dsn: AVP1
username: sa
password: pass
Can anyone please help me before I pull all my hair out?
I am using a 64 bit Arch Linux that is completely up to date.
What could be causing isql to fail. I've tried every solution I've seen so far for this problem but none of them are actually working for me. Do I have to recompile FreeTDS or something?
Ok, I have also verified with strace that it is finding the configuration file, as shown by this excerpt:
open("/etc/odbc.ini", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=159, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc71fe09000
read(3, "[AVP1]\n Description = ODBC "..., 4096) = 159
If anyone has gotten tsql to work but has searched far and wide on the Internet and has troubleshooted their configs and still has not been able to get isql to work check your server logs.
I have been troubleshooting a Xubuntu 12.04 unixodbc install and config for a week now and tried everything possible to get it fixed when I decided to check my windows server event viewer to see what was happening when the request was coming into the server or if a request was even coming into the server and discovered that the problem was that I couldn't get into a specific database. I was able to get into SQL Server ok but not the actual DB I had listed in my odbc.ini file.
Here is the specific text in the event log "Login failed for user 'ePMX'.
Reason: Failed to open the explicitly specified database. [CLIENT:
192.168.27.25]".
What sparked my interest was the word "explicit". So I simply commented out the Database = <DB Name> and suddenly everything worked and I got the SQL prompt after untold hours of researching and trying everything possible.
So if you are having trouble using unixodbc don't forget to troubleshoot the server side of things as well the client side because I have seen tons of posts where people had the exact same problem I was having but there was never any response to how to resolve it so I am guessing that a large number of the people that were having the issue were Server side issues.
For a great troubleshooting tool use osql rather than isql(osql actually in fact uses isql to connect) because it will go through the connection process step by step and give you details about where the failure occurs. It is used the same way you use isql:
osql <DSN> <user> <password>.
So as I said be sure to check your server logs if you have tried everything else and have been unable to figure out what the problem is.
Ok, I finally figured it out after only 2 straight days of banging my head against the wall.
I'll try to give as much info as possible so that if someone finds this in the same situation I was in, they'll find this useful.
[earlz#earlzarch ~]$ cat /etc/odbc.ini
[AVP1]
Description=ODBC connection via FreeTDS
Driver=/usr/lib/libtdsodbc.so
Server=192.168.0.100
UID=sa
PWD=pass
Port=1232
ReadOnly=No
[earlz#earlzarch ~]$ cat /etc/odbcinst.ini
[TDS]
Description = v0.60 with protocol v7.0
Driver = /usr/lib/libtdsodbc.so
Driver64 = /usr/lib
Setup = /usr/lib/libtdsS.so
Setup64 = /usr/lib
CPTimeout =
CPReuse =
FileUsage = 1
[earlz#earlzarch ~]$ cat /etc/freetds/freetds.conf
[global]
tds version = 8.0
initial block size = 512
swap broken dates = no
swap broken money = no
try server login = yes
try domain login = no
cross domain login = no
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
[TDS]
host = 192.168.0.100
port = 1232
tds version = 8.0
and if your lucky, after that:
[earlz#earlzarch ~]$ isql -v AVP1
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
[ISQL]ERROR: Could not SQLConnect
[earlz#earlzarch ~]$ isql -v AVP1 sa pass
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
I did not have to set any kind of environmental variables and I didn't have to manually compile anything either with Arch Linux 64bit (date April 7th, 2010). After getting isql to work, Rails immediately connected to the database also. Now I just have to figure out why db:schema:load isn't working, but thats another question :)
Also, notice the only real difference between this set of files and the last is in /etc/odbc.ini I set the Driver field to be the actual file name of a driver rather than named for some configuration entry.
When building FreeTDS, current versions of SQL Server need TDS protocol v8 (http://www.freetds.org/userguide/config.htm):
./configure --with-tdsver=8.0 --enable-msdblib

Resources