Using embedded Derby with JRuby on Rails - ruby-on-rails

Attempting to use JRuby 1.2.0 and Rails 2.3.2 with an embedded Derby database. I've copied derbytools.jar and derby.jar to $RUBY_HOME/lib, yet rake db:migrate still gives:
The driver encountered an error:
cannot load Java class org.apache.derby.jdbc.ClientDriver
Aaaand... I played a hunch and figured it out. So, I'll post this here in case somebody else runs into the same problem I did.
Almost all the documentation I found online has the following database.yml configuration for Derby:
development:
adapter: jdbc
driver: org.apache.derby.jdbc.ClientDriver
url: jdbc:derby:[db];create=true
username: xxx
password: xxx
This probably works fine for a client/server setup, but for an embedded Derby setup, you need this:
development:
adapter: jdbc
driver: org.apache.derby.jdbc.EmbeddedDriver
url: jdbc:derby:[db];create=true
username: xxx
password: xxx
Note the 'EmbeddedDriver', and not 'ClientDriver'.

Going to answer, because I hate seeing that red block in my profile.
There's also a subtle bug in ActiveRecord-JDBC when you use embedded derby -- if you don't give it a username and a password, nothing works. I've tracked down the cause of this bug, and am working on submitting a patch, but if you run into the same problem I did, let me know, and I'll post the code here.

Strange it worked fine for me , on my ubuntu 9.04 box :
i m using only the standard ubuntu packages and my DB configuration is :
development:
adapter: jdbc
driver: org.apache.derby.jdbc.EmbeddedDriver
url: jdbc:derby:[myapp];create=true

The ClientDriver is in derbyclient.jar

Further to Don's answer, I was getting this error when using the ClientDriver without a username/password: The driver encountered an error: java.sql.SQLNonTransientConnectionException: Password length (0) is outside the range of 1 to 255.
Setting username/password in database.yml fixed the problem!

Related

Inexperienced Rails User: Wanting to Connect PostgreSQL to Already Existing App: Server Will not Connect

Optional Background: Hello, I’m quite new to the services on google cloud..well, and web development in general (Let’s say my training thus far is less than 60 hours on the rails framework and I’m not used to MVC or using databases at all. “Good luck kid”, I know.) My task is to deploy a mostly-done rails app that runs on Heroku to Google app engine. The app “builds” (after using gcloud app deploy), but it won’t connect to any database. Some of the files for doing that that I think should be there are missing. Namely, the config/database.yml file.
I thought it would make sense to just learn with a tutorial how you even connect a database on Google to an already existing rails app. I know rails generally just sets you up with a sqllite system automatically. But how do I write my own database.yml file to work on google app engine? And later on, how do I import all the information from our prior database...questions questions
Question Starts Here So, I started following this tutorial on the google cloud shell:
Pg 1: https://cloud.google.com/ruby/getting-started/tutorial-app I started here.
Pg 2: https://cloud.google.com/ruby/getting-started/using-structured-data Got here and decided on postgresql as my choice
Pg 3: https://cloud.google.com/ruby/getting-started/deploy-postgres and ran into an error where it tells me to call $: rake db:migrate
Here is the error message:
:~/projects/Bookshelfapp/getting-started-ruby/2-postgresql$ rake db:create could not connect to server: Connection timed out
Is the server running on host "35.193.145.252" and accepting TCP/IP connections on port 5432?”
Am I not connected to the database at all? I'm looking at my database.yml file.
Here is the format I was supposed to follow:
postgresql_settings: &postgresql_settings
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
password: secret123
host: 173.194.230.44
database: bookshelf
Here is how I went for it: My database.yml file:
postgresql_settings: &postgresql_settings
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
password: [my password is here]
host: 35.193.145.252
database: bookshelf
development:
<<: *postgresql_settings
production:
<<: *postgresql_settings
test:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/test.sqlite3
Here is where I found the username and password:
username and password page
And I picked this IP address for the host:
IP address page
What am I doing wrong with my life? I haven’t assigned a static IP address to the VM, but I don’t really understand why I would do that.
Could anyone offer any suggestions of what to check for. I spent quite a bit of time going back into early steps and trying to find where I might have misstepped, but my inexperience isn’t offering too many solutions. Even routes to understand this problem better conceptually might help. I am not sure if I don’t know how to use some tool or if I have a big concept missing.I have never done anything like this before.
This issue seems similar to the following past questions, but I'm not sure how much they match up with mine:
question 1
question 2
question 3
question 4
If that error appears, it means the virtual machine where the database is hosted is not being accessed correctly.
The IP address you need for connecting to the database is indicated when you use SSH to access the PostgreSQL instance you created with the launcher (check the IP address that appears under the Bitnami logo).
You also have to open the server port for remote access, in this case 5432, which can be done with this command:
sudo ufw allow 5432
If you don't have ufw installed in the virtual machine, use apt-get to get it.
After that, restart the server to apply the changes, just in case:
sudo /opt/bitnami/ctlscript.sh restart
Besides, the firewall rule indicated in the tutorial is not well prepared, you have to remove the target-tags part. That way, you will be able to connect to the PostgreSQL database and continue the tutorial.

Rails, Postgres, EC2/AWS: Database Config Does Not Specify Adapter Error

I'm trying to connect my Rails app to an EC2 instance that contains a PG database. I've already checked with Navicat that I can connect to the database given the EC2 details. The issue is that when run locally the Rails app can't be viewed; it throws the error "database configuration does not specify adapter". A similar issue is thrown when I try a database migration. I haven't even tried to push this up to my Rails EC2 since it isn't working locally.
My database.yml file looks like this:
production:
adapter: postgresql
encoding: unicode
database: postgres
host: ec2-54-197-115-117.compute-1.amazonaws.com
pool: 10
port: 5432 (have both included and removed this line)
username: a database username for security
password: the password associated with that user
My gem files include the gem pg.
For the database name I just wrote what it had in Navicat, but perhaps there's an official name associated with it I should be using; if so, how would I find it? The host I got from the EC2 details. And the username and password were the ones I set with the postgres database via unix.
Thanks in advance for any insight!
Edit:
Fixed!
Fixed! I had forgotten to create an actual DB after setting up the PG; I changed the name in my database.yml file to reflect the new db name. Also, I needed to set on my Rails app environment directly (I thought Apache did this automatically w/Passenger) with "export RAILS_ENV=production". I thought it was still broken when I restarted my server and nothing had changed, but I just had to restart the console. Hope this helps someone else out too!

Failing to access environment variables within `database.yml` file

I have the following developement section of my development.yml file:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
When I open a rails console via bundle exec rails console and type ENV['APP_USER_POSTGRES_PASSWORD'] I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with
PGError FATAL: password authentication failed for user "app_user"
This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...'], but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.yml file.
Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.yml file is being loaded?
Update: Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.
Ah, finally figured out the simple solution - it accepts embedded Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
Short and quick solution if you are running a Rails version > 4.2 Run the following command:
spring stop
..then run rails console or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
Previous versions of Rails didn't have this issue since they didn't use Spring server.
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
Within rails console:
puts ActiveRecord::Base.configurations
...or using awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
bin/rails runner 'puts ActiveRecord::Base.configurations'

rake db:create fails, authentication problem with postgresql 8.4

first things first, please excuse my utter noobness. I really tried to find a solution out there, but now i'm stuck and completely clueless.
i'm trying to deploy a rails 3 app on a distant server ; when developping on my local VM, no problem showed. But now, when i try to run
rake db:create
it fails, with error (here translated, since i'm french):
FATAL : password authentication failed for user <<mylogin>>
here's my database.yml :
login: &login
adapter: postgresql
username: mylogin
password: mypassword
host: localhost
port: 5432
encoding: UTF8
development:
<<: *login
database: somesite_development
test:
<<: *login
database: somesite_test
production:
<<: *login
database: somesite_production
the user "mylogin" has been created postgre-side with the command-line tool "createuser". It's authorized to create dbs.
postgresql.conf configures the server to listen on localhost.
I've tried many things with pg_hba.conf, none worked - whatever the method used (ident, password, md5) for user "mylogin" on 127.0.0.1, authentication fails - though i've never had problems connecting / creating dbs with psql.
any clue ?
EDIT: okay, found out how incredibly stupid i've been... the password for my user was simply not set !
I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I was also stuck on this problem for a really long time, and went to a variety of links (including the ones offered in this post) to try and find the answer, but to no avail. However, the solution is very simple. While many of the other responses were on the right track here are the exact steps to solve the problem:
Open your pg_hba.conf file in a text editor of your choice. (It is located in /etc/postgresql//main)
Navigate to the line that reads:
# "local" is for Unix domain socket connections only
and paste below it:
local all all trust
This will trust all unix users trying to connect to the psql server on the local machine. (Read documentation at top of page for further info about function of columns)
Save the pg_hba.conf file and exit the text editor.
Restart the Postgresql server by running the command:
service postgresql restart
Now try and start a psql server by running:
psql -d -U (or "psql " for short)
You should be able log in with no problem.
*Note:
All this assumes that you have have a valid psql username for logging in. If you don't follow the links below to set one up:
Setting up a user:
http://erikonrails.snowedin.net/?p=274
Making sure you have a valid postgres user:
http://archives.postgresql.org/pgsql-novice/2002-08/msg00072.php
Listing all existing psql users:
If you are looking for a "LIST USERS" or "DISPLAY USERS" command then try:
"select * from pg_user;" (when logged in to psql)
Good luck!
okay, found out how incredibly stupid i've been... the password for my user was simply not set ! I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I had same problem. In my case it was because in my database.yml file the username started with capital letter but in database it was all lower case.
Solution: When creating user from postgres command line, it converts all letters to lowercase. For using capital letters, double quotes must be used.
Example:
create user AlbertEinstein; result = alberteinstein
vs
create user "AlbertEinstein"; result = AlbertEinstein
Here are some concise instructions that should work for you
http://www.cyberciti.biz/faq/psql-fatal-ident-authentication-failed-for-user/.
Basically you need to set the authentication method for localhost to 'trust'.
You need to give "myuser" in postgresql the privilege "Can create database objects".
In pgadmin, it looks like this:
After calling rake db:create, you can take away this privilege.
In my case, I found that a later host permissions rule line in the pg_hba.conf file had over-ridden the earlier local line. I was correctly configuring the local line to use md5 authentication but the host line was set to ident which I changed to md5
As other have emphasized here, note that using trust is an insecure method, so you shouldn't use it in any production-style deployment.

Database not being selected in rails project when attempting to rake db:migrate

Working with a rails app, having some manner of weird database / rake issues.
When I execute:
rake db:migrate
I am getting the following error:
Mysql2::Error: No database selected: SHOW TABLES
(See full trace by running task with --trace)
The trace isn't revealing much useful information. Can be seen here: http://pastebin.com/WdsguudC
The config file looks right, and the user is getting logged in, or I would have gotten some kind of access error. The database exists, the user has correct permission, and I can access and manipulate it manually. I have done a bunch of googling, and haven't found anything helpful. Not sure if there is any other code that needs provided, because this seems like fairly low level problem.
after all that it was a spacing issue in the yaml.
Note that ruby has exchanged its YAML parser in a recent 1.9.2 version.
This might also cause this problem.
In order to switch back to the old YAML parser syck, use this in boot.rb:
require 'yaml'
YAML::ENGINE.yamler= 'syck'
Well, it is a common issue for us beginners. This issue comes from the moment when you create your new project in rails. Let’s say to have an example
$ rails new toy –d mysql
After you do the bundle and start your server, most likely you will have an error. To correct it you need to go to your database.yml and modify the following:
Add a password in the password field as shown below, this is the password you use to secure mysql.
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: mypassword
socket: /tmp/mysql.sock
Also, comment out the database adding a hash tag (#)before the name as shown below
development:
: *default
database: #toy_development
Then restart your command line and go to the root of your application and type:
$ rails s
You have to see the Ruby on Rails welcome page..
After, you need to create a database.
Create a DATABASE.
The issue message is saying that not DATABASE is selected. It is because I didn’t create one. When you work with MySQL you have to create one, so:
Go to the root of my application and type:
$ mysql –u root –p
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)
Note: This example works wit a project called toy and the user I wanted to grant privileges is mark and the password I’ll give is 45mark. Below you will see where I apply these elements. Remember to apply your own elements on each part of the statement.
Create and user for this project
Once you are in, you will see the pointer (mysql> ), so type after it:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'#'localhost' IDENTIFIED BY '45mark';
Then type:
mysql> exit;
Check that it is working by typing:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
Open database.yml file and configure what is needed and fix as required. In my case I will chance the username to mark and the password to 45mark
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: mark
password: 45mark
socket: /tmp/mysql.sock
- Also, REMOVE the hash tag (#) added before
development:
: *default
database: toy_development
Save it.
Go to the root of the application and type
$ rake db:schema:dump
Done!!
I hope this helps. Happy coding!!
Thanks
I had the same issue with ruby 1.9.2-p180 , upgraded to p290 and it works
Just restart the server; in the command line:
Press Ctrl + C
execute:
rails s
I had a similar error when i typed rake db:schema:dump and it turns out that I just have to comment out all the databases on my yaml file except my development one.
Give a try to this.
rake db:test:prepare
Install this to see if you have actually created a table or not. Open the "development.sqlite3" in db folder
http://sqlitebrowser.org/
Its a simple error checkout the entire database.yml file and see that where is default decription is given database name is given or not if not then look below it there will another development name is also given where configuration of database is use check that give your database name in it
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 12345
host: localhost
development:
<<: *default
database: db_name
One potential cause is that there is a DATABASE_URL environment variable defined.
$ echo $DATABASE_URL
=> mysql2://root#localhost:3306
If you get a similar output to the above url (i.e., the database name is not present), then you might want to add the database name to the string or unset the env var.
$ export DATABASE_URL=mysql2://root#localhost:3306/my_rails_app_development
$ unset DATABASE_URL
If you unset the var, you probably want to specify the database details in database.yml instead.

Resources