unixodbc and freetds setup on OSX and Rails 2.3.x - ruby-on-rails

I'm trying to set up unixodbc on OSX (10.7.3), but it looks like iODBC is on the way.
My config/database.yml:
development:
adapter: sqlserver
encoding: UTF8
mode: odbc
username: user
password: passwd
dsn: MY_DSN
ruby-obdc is installed and in the Gemfile:
gem 'ruby-odbc', :require => 'odbc_utf8'
/usr/local/etc/freetds.conf:
[MY_SERVER]
host = host.bla.com
port = 1433
tds version = 8.0
client charset = UTF-8
/usr/local/etc/odbc.ini
[MY_DSN]
Driver=/usr/local/lib/libtdsodbc.so
Description=Sql Server Local
Servername=MY_SERVER
Port=1433
Database=my_database
When I run script/console and try to access any model:
Product
ODBC::Error: IM002 (0) [iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded
It seems iODBC is being used to connect to the database, when I want to use unixodbc. How can I make my rails app use unixodbc instead of iODBC?

Solved by compiling ruby-odbc against Homebrew libraries. This works if you are on OSX:
gem install ruby-odbc -- --with-odbc-lib=/usr/local/lib

iODBC, maintained and supported by my employer, has shipped as part of Mac OS X since Jaguar (10.2.x).
You'll be better off updating iODBC with all the latest patches (Apple tends to be a bit behind on these), than shifting to UnixODBC. You may also find some benefit in the Ruby+ODBC and Ruby-on-Rails docs we've published.
Last, it's best to keep all your ODBC configuration in the default file locations for Mac OS X --
/Library/ODBC/odbc.ini
/Library/ODBC/odbcinst.ini
/Users/*/Library/ODBC/odbc.ini
/Users/*/Library/ODBC/odbcinst.ini
You can create symlinks from anywhere else you may want to have these files, e.g. --
ln -s ~/Library/ODBC/odbc.ini ~/.odbc.ini

Using Homebrew. Needed to use:
gem install ruby-odbc -- --with-odbc-dir=/usr/local/Cellar/unixodbc/2.3.2
I didn't notice this earlier but in Pedro's answer he has with-odbc-lib. not sure if that works for his environment, but i needed this to be dir, because i kept getting a checking for sql.h... no error.

Related

Rails 4 ActiveRecord throws PG::UnableToSend on Ubuntu 13.04

We have a Ruby v.2.0.0-p247 on Rails v4.0.1 application using pg gem v0.17.0.
The application runs smoothly under Mac OS X Mavericks v10.9 with PostgreSQL Server v9.2.4 installed using HomeBrew but it throws the following exception under Ubuntu v13.04 using PostgreSQL Server 9.1:
PG::UnableToSend: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.
The exception occurs after transactional queries (form submission).
I tried the following with database.yml:
Adding reconnect: true
Adding port: 5432
Adding socket: /var/run/postgresq/SOCKET_FILE
And tried the following with PostgreSQL configuration under Ubuntu:
Disabling SSL.
Changing TCP keepalives parameters to pump timeout.
Changing log level to DEBUG and search for possible errors on PostgreSQL Server.
Also tried:
Downgrade to pg gem v0.16.0.
Update all Ubuntu 13.04 packages to latest versions.
What could possible be wrong?
UPDATES:
12/03/2013: Some suggested checking firewall settings. ufw status said that ufw is disabled.
12/08/2013: After trying out with a vanilla Rails app and a lot of mangling with the current application, the problem is originating from rails4/activerecord-session_store gem. Line 47 in lib/active_record/session_store/session.rb is the culprit.
This basically happens when you use an old version of launchy , and as per this issue on launchy's git repo quoting #infertux
In the rare case when exec fails to run the command - typically when the file cannot be opened raising Errno::ENOENT - Launchy would raise an exception but not showing any output
You can check your Gemfile.lock to see if you're using a version of launchy below 2.4.1, and I doubt that you're using letter_opener Gem in your development environment which depends on launchy so updating letter_opener to 1.2.0 will update launchy to a version above 2.4.0 mostly 2.4.2 which has this issue fixed
All credit goes to #infertux
It would really help if you provided your database.yml file
The default connection method is a unix domain socket, not to be confused with a TCP/IP socket. The unix domain socket connection method is used by default.
You should check that the unix user that you are trying to run rails under has sufficient permissions to access the domain socket (typically at /var/run/postgresql/.s.PGSQL.5432)
Try typing the following as your rails user:
psql
If you get a database connection error then its likely a permissions problem if postgres is actually running.
You can check your /etc/postgresql.conf file and have postgres configure the group and permissions on the socket when it starts:
unix_socket_directory = '/var/run/postgresql' # dont worry if yours is different
#unix_socket_group = '' # default is usually ok
#unix_socket_permissions = 0777 # uncomment this if needed
Another option is to add the user to the group that has write access to the socket, vs allowing all users on the machine access with the 0777 permissions setting above. You may want to create a postres_users group for this purpose if the default Ubuntu groups provide insufficient granularity for your needs.
It looks to me like there's confusion about the connection method. PostgreSQL supports the two methods:
Socket
TCP/IP
These are completely, utterly different. :-)
In your question, you show a socket setting, but mention TCP. I suggest to focus on each of these two ways of connecting in isolation and see which produces results. I.e., create two version branches pg-socket and pg-tcpip just to make things clear. Then, clean up your config file and attempt to connect via both methods.
Often, a socket connection is easier because you just need to know the pathname of the socket "file". No fireware settings are necessary (because no TCP/IP networking is involved.)
Update your question after doing this and tell us how each of the two methods did for you.
Have you set keepalives as specified here http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS similar issue was solved with it at: https://bitbucket.org/ged/ruby-pg/issue/170/pg-unabletosend-connection-timed-out

Issues starting a rails server

I am working on an application that makes use of Ruby on Rails. There are 5 of us in the group and so we are using GitHub. I have rails setup on my computer and am able to create my own projects and databases from scratch but when I clone our existing project from GitHub and try to start the server I run into all sorts of issues.
I should mention that I am working on a Windows 7 machine and the initial project was created on a Linux machine. I imagine this could be the source of some of the issues.
Bottom line, when I am in the necessary folder and I type rails s I get a lengthy error message that starts with:
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32
/lib/mysql2/client.rb:44:in `connect': Access denied for user 'root'#'localhost'
(using password: NO) (Mysql2::Error)
There is a ton more information that I could provide but I'm not sure what all is needed to help solve this issue. I am a beginner (both with StackOverflow and RoR) so I apologize for not being more clear and concise.
Thanks in advance.
Additional Information:
mysql: Ver 14.14 Distrib 5.5.30, for Win 64 (x86)
ruby: Ver 1.9.3p125
rails: Ver 3.2.0
mysql gem: Ver 2.9.1
mysql2 gem: Ver 0.3.11
It seems that your rails app is trying to log into MySQL with the wrong credentials.
Update the file config/databases.yml to set the rights credentials.
Note that this file should not be checked into version control since it is different for each member of the team.

How can i setup the path for heroku postgresql app?

I just installed the heroku PSQL app (v1.0) and i'm having trouble being able to connect my rails apps using the gem pg "0.1.4". I already added the path PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH" to my .profile and my .bashrc files, but nothing seems to allow me to run psql simply by calling "psql". I had success using "psql -h localhost". When i go for "psql" i get:
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I'm using mountain lion.
Still pretty sure that both of the existing answers are answering the wrong question. The author mentions right in the title that he is having trouble with PATH, not connecting to his DB or configuring rails. This is the situation I got into, and this is my solution.
After getting postgres.app running and setting PATH in my .bashrc file as directed in the postgress.app documentation - http://postgresapp.com/documentation :
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
The problem was that this path setting was not taking effect after restarting terminal.app - running which psql was returning the copy in "/usr/bin/psql", this is the copy that comes installed with Lion and Mountain Lion, not the new version installed in "/Applications/". It even says in the Postgres.app instructions "run which psql to tell that the correct version is being loaded".
Anyway - the odd thing I found was that after I ran:
source .bashrc
Then the command "which psql" would return the correct version located in /Applications/.
At this point I was stumped and had to get some extra help. The guys I tapped also thought it was quite odd too, however they quickly found out that neither the .bashrc OR the .profile files were being loaded. This is very strange, I have not seen this on any of my other macs running leopard through lion.
Now finally the solution- I am not sure this is proper, but it did permanently fix my problem. We found that their was one profile file being loaded into the terminal - the .bash_login file. In the end the solution was just to use the .bash_login to source the .bashrc file. Here is the edit to .bash_login:
source $HOME/.bashrc
And that did it.
Anyway I can't say that this is exactly the fix that diego needed / was looking for, but it is definitely the problem for me.
You'll need to add host:localhost to any database configuration you're using in your development environment (read development / test/ production / etc):
config/database.yml
development:
adapter: postgresql
encoding: unicode
database: my_awesome_app_development
pool: 5
host: localhost
username: my_awesome_app_development
password:
Edit: also see Repairing Postgresql after upgrading to OSX 10.7 Lion
I know this is an old question but I just had the same problem. The issue is that there is a typo in the documentation. The app is Postgres93.app, not Postgres.app, so the path should have Postgres93.app:
PATH="/Applications/Postgres93.app/Contents/MacOS/bin:$PATH"
It's amazing how many posts there are about this and the typo doesn't seem to have been noticed yet. Changing that was all it took to get psql and which psql to work correctly for me.
Your PATH might be wrong. To find out the PATH of your psql script (on mac) open the sql shell script from your finder in Applications/Postgres installation. This will give you a hint as to where it is installed. That opened a window which told me it is located here: /Library/PostgreSQL/8.4/scripts/runpsql.sh
Then, I set the PATH variable from the terminal window by typing:
$ PATH="/Library/PostgreSQL/8.4/bin:$PATH"

Installing Sphinx on OS X Lion with Postgresql

So I installed postgresql and the thinking-sphinx gem. I cd into my sphinx download folder and try to run ./configure --with-postgresql
I receive the following message:
#
ERROR: cannot find MySQL include files.
Check that you do have MySQL include files installed.
The package name is typically 'mysql-devel'.
If include files are installed on your system, but you are still getting
this message, you should do one of the following:
1) either specify includes location explicitly, using --with-mysql-includes;
2) or specify MySQL installation root location explicitly, using --with-mysql;
3) or make sure that the path to 'mysql_config' program is listed in
your PATH environment variable.
To disable MySQL support, use --without-mysql option.
#
What should I do? I have also tried install sphinx using brew, but that hasn't worked either. I'm desperate. I just spent three hours trying to configure this, and I have gotten nowhere.
My sphinx.yml looks like this, and i'm not sure if it's correct:
development:
port: 9310
bin_path: "/usr/bin"
searchd_binary_name: searchd
indexer_binary_name: indexer
test:
port: 9310
bin_path: "/usr/bin"
searchd_binary_name: searchd
indexer_binary_name: indexer
production:
version: '1.10-beta'
I used homebrew. According to the docs at thinking-sphinx, you need the mysql extension even if you are using postgres.
I mucked around with a few configurations but sphinx kept looking to connect to MySQL.
This worked for me:
brew install sphinx --mysql --pgsql
Which configured:
./configure --with-mysql --with-pgsql
Then it seemed to work.
See:
https://github.com/pat/thinking-sphinx
I agree with Tass. You wold run Sphinx's configure script with --without-mysql & --with-pgsql. I have PostgreSQL installed in /usr/local; therefor, my configuration options would be:
shell~> ./configure --without-mysql --with-pgsql=/usr/local

Installing Postgres on windows for use with Ruby-on-Rails

Currently I get the following error:
PGError (FATAL: password authentication failed for user "postgres" ):
when my app tries to access the database.
I wanted to test my SQL calls against postgres as my app regular breaks when pushed up to production on Heroku because of the stricter requirements of postgres (which I think is probably a healthy thing) over sqlite 3. So I have a similar request to this for a not out of date tutorial for installing postgres for use with ruby on rails on windows (7) please. I thought it'd be a quick 20 minutes of downloading and installing but 2 and a bit hours later and I don't think I'm very close yet. So far I've:
Downloaded and installed Postgres Version 8.4.8-1 from here
Set my environment variables such that Path (for User) is: C:\Ruby192\bin;C:\Program Files (x86)\PostgreSQL\8.4\bin
Gemfile:
gem 'pg', '0.11.0' # instead of gem 'sqlite3', '1.3.3'
ran bundle install for my rails app seemingly successfully, but haven't found easy way to validate installation yet.
set database.yml as suggested here to:
development:
adapter: postgresql
database: db/development
username: postgres
password: secret
host: localhost
encoding: UTF8
pool: 5
timeout: 5000
I know I need to set up a user name and password for Postgres, maybe also start the postgres server, connect to it(?) and put in my local IP address I'll connect to it on into a config files somewhere and then edit one of the other .conf files in 'C:\Program Files (x86)\PostgreSQL\8.4\data' etc...
I think Rails has made me soft, am I over thinking things or is it actually fairly tricky to set up and I should just go back to Sqlite3, for which there's also the awesomely useful SQLite Manager, Firefox plugin?
I'm still searching for a beginners guide to installing and using Postgres for rails but so far have only been confused by most of the stuff I look at / tried following like this, this, this, this, this(for Snow Leopard), this(linux).
Any pointers would be much appreciated. Thanks!
James
An approach to installing Postgres on windows 7 for use as PostgreSQL database for a rails 3 (3.0.7) project.
Preamble (you can skip this bit)
So the first thing to point out is that Postgres is not just a different file extension from .sqlite3, it's a whole mechanism for managing your databases. As such it has a client/server model, of which you'll need to set up both to use Postgres as the database for your rails app.
Motivation for going through considerable pain of Postgres setup versus almost effortless sqlite setup: if you're deploying to Heroku, they're currently using Postgres so some of your SQL calls that are fine on sqlite3 will break when used with Postgres. It's much easier to debug postgres locally rather than when it's on Heroku's servers.
So I did the following things:
(Disclaimer: I may have forgotten to include some of the things I did... it took me over 48 hours of on and off pain to get it to work... if the following advice doesn't work for you then the huge (2300 pages!!) but very thorough Postgres documentation should help. I'd recommend downloading this anyway if you're serious about using Postgres as it has a lot of material that I've only just begun to understand the significance of.)
(Second disclaimer: I have almost certainly broken 20 sensible Postgres guidelines and exposed security holes in the Postgres database whilst doing so. If there any obvious things an experienced Postgres user disagrees with, please edit my post.)
.Step 1. Download and install PostgreSQL v9.0.4-1 from here because here said only 9.0.x would be supported on windows 7. I kept all the default options and just used 'secret' as the password when prompted by the Postgres installer for one (again not entirely sure what the consequences of sharing that info on the internet is... will soon find out I'm sure). You'll need this password in step 3.
.Step 2. Change environment variables such that Path (for system, not user (I'm not sure if this is significant or not)) is: C:\Program Files\PostgreSQL\9.0\bin
(n.b. I'm on 64-bit windows hence it not being installed for 32-bit in 'C:\Program Files (x86)\PostgreS...')
Don't forget to change access rights to folder PostgreSQL\9.0 and remove any default readonly rights on the folder or content.
(You may also need to restart your computer for these to take effect - thanks #Gavin -although not likely).
.Step 3. Test Postgres installation by trying to create a new database:
From command line: createdb -U postgres mydb_as_postgres.
You should be prompted to enter the password now, if you're not it may be that you need to start the server first (I can't remember whether I needed to do this or not). The easiest way is through pgAdmin III, which should be 'pgAdmin3.exe' in a folder somewhere like C:\Program Files\PostgreSQL\9.0\bin. Once you've started pgAdmin III there should be a panel on the left called 'Object Browser'. In this there should be a tree with:
Server Groups > Servers > PostgreSQL 9.0 (localhost:5432)
Right click on 'PostgreSQL 9.0 (localhost:5432)' and select 'Connect'.
The createdb -U postgres mydb_as_postgres command should create a new databse called 'mydb_as_postgres' which you can check by firing up pgAdmin III and double clicking on 'PostgreSQL 9.0 (localhost:5432)'. Under this there should be:
Databases (2) which should list 2 databases called mydb_as_postgres and postgres
I called it _as_postgres because the -U postgres part of the command tells Postgres to create the database with the postgres user as it's owner, which you need to specify when you're not signed in as the postgres user. I have all of my files stored as 'AJames' user though so if you're the same and want to keep developing your app when signed in as a different user you need to create a Postgres 'role' for that user now (see step 4).
.Step 4. Through pgAdmin III. Right-click on Login Roles (which for me is in):
Object Browser > Server Groups > Servers > PostgreSQL 9.0 (localhost:5432) > Login Roles
Right-click on Login Roles and select 'New Login Role...'
in Role name, put in your operating system user name, which for me is AJames,
and fill in your password under the 'Role Privileges' tab, I checked all the boxes, but an experienced postgres user would likely strongly recommend to only check the 'inherits rights from parent roles' and the 'can create database objects' But I'm not an experienced user and just want to debug Rails SQL calls in Postgres so I also checked the 'Superuser' and 'Can create roles', just in case.
.Step 5. You should now be able to create a new database without being signed in as the postgres user. Try typing:
createdb mydb_as_user
Hopefully this should work for you.
.Step 6. Okay, so you've got a development.sqlite3 file in your rails 'db/' directory. Initially I was going to set the next test as converting this from sqlite3 to psql.
I couldn't get this to work though but I left my attempts here as the solution I used required having the data in a Rails app on Heroku.com (see instead the solution from step 7 onwards). For those who only have a local app and no data in Heroku, they can't use the same approach, so they might need to explore something like this:
x6.1 First, test 'psql' by trying a command from your command line like:
psql mydb_as_user
this should display something like below (after you've typed in your password):
C:>psql mydb_as_user
Password:
psql (9.0.4)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
mydb5=#
x6.2 try entering:
CREATE TABLE users_table (id integer, "name" text);
It should display:
CREATE TABLE
mydb5=#
If you check in pgAdmin III, you should see the table there under:
Object Browser > Server Groups > Servers > PostgreSQL 9.0 (localhost:5432) > Databases > mydb_as_user > Schemas > public > Tables > users_table >
x6.3 Okay, next to try the conversion. Downloaded sqlite-shell precompiled binary for windows.
x6.4 Create a new directory, I used 'C:\temp' and put the sqlite3.exe and your development.sqlite3 files in it.
x6.5 Use the following commands (which are from here) to dump the development.sqlite3 database into Postgres.
sqlite3 development .dump | psql development2
you might get an error like:
psql: FATAL: database "development2" does not exist
x6.6 so I went into pgAdmin III and made a development 2 database, tried the command again and got:
ERROR: syntax error at or near "PRAGMA"
LINE 1: PRAGMA foreign_keys=OFF;
^
BEGIN
COMMIT
Like I said, I couldn't get it to work. I'm sure there's a way of getting round that error but I thought of a different way and so I instead used this solution (which requires a Heroku account to have your data and does the conversion from sqlite3 to psql using the Taps gem (I believe):
.Step 7. in pgAdmin III I created another database. Under the properties tab I set name: 'development', owner: 'AJames' (replace this with your own Windows user name). And under the privileges tab, set role: 'public' and checked the ALL option (thought this resets to unchecked so I'm not sure that's necessary).
.Step 8. add
gem 'pg', '0.11.0'
to your gem file. You'll probably also want to remove the:
gem 'sqlite3'
at this point too.
.Step 9. set database.yml as suggested here to:
development:
adapter: postgresql
database: db/development
username: AJames # replace this with your own user name
password: secret # replace this with your own password
host: localhost
encoding: UTF8
pool: 5
timeout: 5000
If you are working on an open source project and don't want your password to be made publicly available, have a look at some of the answers to Securely providing the database password in a Rails app.
.Step 10. from command line in your rails app's root directory run:
rake db:migrate
This will create the new schema and all the tables in the Postgres database.
.Step 11. run heroku db:pull from your command line (again from in the root directory of your rails app) to pull all your data down and into your new empty Postgres database. I think at this point your taps gem will be doing this work for you.
.Step 12. Hopefully there is no step 12! ...and it should now be working for you. Happy RoR PostgreSQL debugging! Please edit, or let me know, if there are any errors in this.
Also, here's a list of additional stuff that might be interesting/useful:
This is a blog post about Postgres passwords, what they're for,
why you need them, how to change them etc.
This, under 'Creating a spatial database', is useful for newbies to
understand what pg_hba.conf is about and the second link that
Reno gave above, under the 'Using pgAdmin III GUI' is useful to
testing to see if postgres is actually working, before trying to fit
it with Rails (i.e. try creating a database and putting a table and
some data into it).
In the huge but very comprehensive Postgres documentation, I'd
start on page 58, 'I. tutorial'. Then on pdf page 431(!) there's
'Chapter 17. Server setup and operation' that I also found useful.
the answer from AJP is the correct one with just a small re-config.
The line
database: db/development
does not work for me.
I have to change it to
database: development
I use this for ruby on rails for the command
rake db:create
and
rake db:migrate
to work
None of your links appeared to be the (arguably) most useful documentation - the official postgresql docs. I recently configure a Linux Mint box with postgresql and django using a combination of those documents and these, though the latter are specific to Linux.
I'd worry more about validating the postgresql side of things, less about rails. That is to say, your question should be "How can I set up and test a postgresql server on a Windows 7 box", moreso than you need to know how to get it locked into rails.
Edit:
Maybe this could also be of use to you - official postgresql wiki with detailed installation guides.
Good luck!

Resources