I need to convert an app from sqlite to Postgresql in order to use search with thinkingsphinx. I have run the following steps what am I missing?
I added the pg gem to my gemfile, ran brew install postgresql, and have configured my database.yml file as follows:
development:
adapter: postgresql
database: example_development
username:
password:
host: localhost
encoding: UTF8
I have also run the commands that homebrew suggests on installation:
If this is your first install, create a database with:
initdb /usr/local/var/postgres
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
What do I put for my username and password? Is there a file I need to edit or do I need to create a database from the command line? There must be something simple that I am missing. I haven't run any custom sql queries, and have stuck to active record defaults.
You need to specify the username and password of the account on the Postgres database you are using.
If you do not have a local postgres installation running, then there is no database to connect to, so you will have to follow some instructions online that outline installing and setting up a postgres database on your development machine.
Related
I am on a Ubuntu 14.04 LTS machine.
I found useful information on how to configure PostgreSQL in Ubuntu for Rails development at help.ubuntu.com, at Heroku and at digitalocean.com.
Putting everything together, all the information seems to converge on the necessity of creating a database superuser with login name that match my Ubuntu user name with: sudo -u postgres createuser --superuser $USER
When time arrives to create a password for the new superuser with sudo -u postgres psql , I am wondering if Rails can use PostgreSQL without setting the password, if this password can and should be different from my Ubuntu account password and also whether database.yml could be a security concern when pushing to Git repository hosting web sites and to Heroku. Indatabase.yml in fact is recorded exactly this kind of sensitive information.
According to Heroku it is necessary "to export the DATABASE_URL environment variable for your app to connect to it when running locally", with: export DATABASE_URL=postgres:///$(whoami)
Is that really necessary? At help.ubuntu.com and digitalocean.com this information is not reported.
Finally I am wondering whether the choice of installing PostgreSQL through the PostgreSQL apt repository would be safe enough or it would be preferable to install the LTS version of Ubuntu.
There are two ways for Rails to set the connection with a database: via config/database.yml or via the environment variable ENV['DATABASE_URL']. See at guides.rubyonrails.org
By default $DATABASE_URL is empty:
echo $DATABASE_URL
If posgresql is installed via PostgreSQL apt repository, in order for Rails to use the pg gem it is also necessary to install the libpq-dev package, otherwise bundle install will fail. See Can't find the 'libpq-fe.h header when trying to install pg gem.
From 'man createuser':
createuser creates a new PostgreSQL user (or more precisely, a role). Only superusers and users with
CREATEROLE privilege can create new users, so createuser must be invoked by someone who can connect as a
superuser or a user with CREATEROLE privilege.
When postgresql is installed, it creates a user postgres with role postgres. It also creates a postgres system account.
So this is why createuser should be run as postgres user in order to connect to postgresql for the first time and add the user $USER (current system user).
It is not necessary to create a password for the new database user. Most people add database.yml to their .gitignore file so it stays out of version control. It is also possible to use .pgpass to keep sensitive
information out of the *.yml file: see postgresql documentation.
It is possible to connect to postgresql only as a database user AND through an existing database.
During installation from the postgresql apt repository, postgresql only creates the postgres user and the postgres database.
The psql command allows the current user to connect to the postgresql database named after the current user. So, if the system user is 'dave' AND there is a 'dave' database it is possible for 'dave' to connect to the 'dave' database with command psql with no options.
If 'dave' is a database user but the database 'dave' was not created, for dave to connect to postgresql it is necessary to specify an existing database with:
psql -d postgres
Alternatively, it is possible for dave to connect to postgresql executing the psql command as the postgres user with sudo:
sudo -u postgres psql
Trying to deploy my rails app to Heroku for the first time, I ran into many problems. The app crashed, and you can look at the logs in the image: http://i.stack.imgur.com/bsx1b.png
Even worse, when I tried to look at my application locally, it failed to work on postgreSQL environment.It worked fine when I went back to use sqLite3 in database.yml. I thought the problem might be that I actually have to install postgreSQL, in addition to adding pg gem and running bundle install.
I went ahead to install PostgreSQL with the one-click installer (Windows 7 64bit). After reboot, a simple rails server or bundle install commands started failing, so I reinstalled the entire ruby & rails.
Now rails server command works fine, and it prompts a different error when I try to look at my app on local environment. So right now I have pgAdmin III and Rails
PG::Error
fe_sendauth: no password supplied
I tried to follow this, but I couldn't find "pg_hba.conf." I guess the answer was based on a different OS.
And now again, the bundler is not working, giving me errors...
Questions:
1) Was I right to install postgreSQL with the one-click installer? Because this caused my ROR to "crash" somehow, and I had to reinstall rails altogether. 2) I think I am going to delete everything related to postgreSQL, reinstall Rails, and start everything from beginning. What are the steps that I have to take? All the references I've looked at do not seem to fit Windows environment.
In my gemfile, I have
gem 'pg'
gem 'thin'
In my database, I have
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: utf8
database: mangfeel_development
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: utf8
database: mangfeel_test
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: mangfeel_production
pool: 5
timeout: 5000
Errors I'm getting at this moment, after installing PostgreSQL:
c:\ruby\myapp>rails server
C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:926:in `report_activate_error': Could not find RubyGem railties (>= 0) (Gem::LoadError)
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:244:in `activate_dep'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:236:in `activate'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:1307:in `gem'
from C:/RailsInstaller/Ruby1.9.3/bin/rails:18:in `<main>'
When I run bundle install:
Gem::InstallError: The 'json' native gem requires installed build tools
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
An error occurred while installing json (1.7.5), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.5'` succeeds before bundling.
So I will just remove pgAdminIII, PostgreSQL, Ruby on Rails entirely, and start from scratch.
I thought deploying wasn't going to be this hard, but moving from SQLite3 to PostgreSQL is taking more time and effort than I thought it would. I would really appreciate some help on this problem.
------------------------------------------------------------------------------------------------------------------------
UPDATE: AFTER REINSTALLING EVERYTHING, I'M DOING THE SETUP AGAIN FROM SCRATCH.
I checked that my SQLite3 version app worked fine. So I moved onto setting up postgreSQL. I got the following error when I first set up my postgreSQL. By setting up, I mean editing config/database.yml and installing the gem 'pg' and removing the gem 'sqlite3.' When I tried to connect to localhost:3000, I got the following error:
ActiveRecord::ConnectionNotEstablished
After some research, I found that there could be more steps to setting the db. So I ran the bundle command,
bundle exec rake db:setup
But then in the console, I got the following error when I ran bundle exec rake db:setup.
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "encoding"=>"utf8", "database"=>"db/myapp_test", "pool"=>5, "timeout"=>5000}
rake aborted!
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
c:/ruby/myapp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:load => environment
When I tried to connect to localhost:3000, I got the above error again, instead of ActiveRecord::ConnectionNotEstablished.
Question: What did I do wrong, and how do I solve the problem?
If you have error:
PG::Error
fe_sendauth: no password supplied
You have good news, your app can work with PostgreSQL database now. (But you need to configure some thing)
After installing PostgreSQL, you can create new rails app using PostgreSQL by command:
rails new my_app -d postgresql
This is database.yml will be created:
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
You see that, three database has username and password field, this is account user in Postgresql, three database in database.yaml file will created by this user. So you need to provide username and password for it. Default after install Postgresql, it created a user with username is postgresql and password is postgresql. You can use it.
If you want to use other user, just open pgAdmin III and create new user by right click on Login Roles and choose New login role to create new user with password. Then put that username and password to database.yaml file.
After that, all you need now, is find and open pg_hba.conf file to make some configure for app can work with PostgreSQL. I'm not using Windows for a long time, so I'm not sure where it is, but you can find in folder you installed PostgreSQL. Example, if you install on E:\ partition, maybe you can find it in:
E:\PostgreSQL\version\data
After you found it, open and find a line:
# "local" is for Unix domain socket connections only
local all all ident sameuser
Change it to:
# "local" is for Unix domain socket connections only
local all all md5
Then restart your Postgresql database. To create database for app, run rake db:create or rake db:create:all. Now your app can using PostgreSQL database now, so happy :).
I am not an expert on Rails, but here is one of my YAML files for a recent deployment i did on heroku with POSTGRES
development:
adapter: postgresql
host: localhost
database: dbname_development
production:
adapter: postgresql
host: localhost
database: dbname_production
Seems like you are missing a parameter called host in case of postgres deployment
Steps to Move From SQLite3 to PostgreSQL on Windows, for an Existing Rails App to Deploy To Heroku:
In your Gemfile, find gem 'sqlite3' and change it to gem 'pg'. Run bundle install.
Download Postgres from here. Choose the one-click installer and select your OS environment (32 or 64 bit). During installation, do NOT choose to install any extra software it recommends you to install. It will possibly mess up your existing Rails and you will have to reinstall Rails.
Now go to config/database.yml, and change it to the following format. The current one is set for SQLite3 and is missing some fields necessary for PostgreSQL.
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
The adapter is different, and there are fields for username and password. The default username and password is postgresql, but if it doesn't work (it didn't work for me), then go to your pgAdmin, create a new login role to make another username/password (check the option to make it a super user) and use that.
4, At this point, you can create a new app by running
rails new my_app -d postgresql
Run your local server, and try connecting to localhost:3000 to see if the welcome page is showing.
5, If everything is working fine, then all you need to do is create a new PostgreSQL database, and migrate your existing models to it. Run
rake db:create:all
rake db:migrate
Done! Now check if your existing app is working fine locally, and try deploying your app to Heroku.
For a new project I'm trying to run postgres locally.
Mac Lion has Postgres installed (9.1.2). I modified my path in .bash_profile (per another SO thread) so now I have no trouble creating a database or user using the psql utility.
I uninstalled and re-installed the pg gem (0.13.2) using env ARCHFLAGS="-arch x86_64" gem install pg per the excellent blog at http://blog.willj.net/2011/05/31/setting-up-postgresql-for-ruby-on-rails-development-on-os-x/
My Gemfile uses the pg gem for development, test, and production. I've run bundle install.
My database.yml looks like this (and the username and database names are correct and I verified I can access the database fr that username using the psql utility):
development:
adapter: postgresql
encoding: unicode
database: ddchart_development
pool: 5
username: ddchart
password:
(same for test and production, except _test and _production. Password is blank, right?)
When I run bundle exec rake db:migrate it aborts:
rake aborted!
couldn't parse YAML at line 8 column 10
Any help would be appreciated!
If you pasted the code correctly, there are errors in the code. For example, there's only one space before "adapter", but two before following lines.
In YAML, indentation matters.
OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword
I'm attempting to get my first "hello world" rails example going using the rails' getting started guide on my OSX 10.6.3 box.
When I go to execute the first rake db:create command (I'm using mysql) I get:
simon#/Users/simon/source/rails/blog/config: rake db:create (in /Users/simon/source/rails/blog) Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"blog_development", "pool"=>5, "password"=>nil, "socket"=>"/opt/local/var/run/mysql5/mysqld.sock"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I found plenty of stackoverflow questions addressing this problem with the following advice:
Verify that user and password are correct (I'm running w/ no password for root on my dev box)
Verify that the socket is correct - I can cat the socket, so I assume it's correct
Verify that the user can create a DB (As you can see root can connect and create a this DB no problem)
simon#/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blog_development;
Query OK, 1 row affected (0.00 sec)
to ensure that this wasn't a charset issue I also tried:
mysql> create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
Note: here is my database.yaml:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password:
socket: /opt/local/var/run/mysql5/mysqld.sock
# host: localhost
Note that I tried switching socket to localhost with no effect.
Any idea on what might be going on here?
Thanks for all the help guys. Looks like the problem had to do with my install of the mysql gem under OSX.
#tim after I proceeded to the next step and got up and going I got an error on the console, so I did a bit of searching and found this helpful thread.
After I uninstalled my ruby gems gem uninstall mysql I installed the proper mysql gems using this command (from the thread):
export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
After executing this one I was able to successfully run rake db:create and proceed.
Thanks!!
It could be a number of things.
Is your database set for utf8 character set?
Is the path to the socket correct, since it varies from OS.
Have you reinstalled the mysql gem? sudo gem install mysql
It might be MySQL, you might want to downgrade the version to 5.0
Other than that, I'm not sure.
You should post here your database.yml
To make your test better, i would try to create a database UTF-8 to see if your database supports utf-8
create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci
Does the blog_development database already exist?
If so, you can just continue to the next step.
Try running ruby script/server in the blog/ directory.
If it doesn't error out, then you should be able to navigate to localhost:3000 and continue the tutorial from here http://guides.rubyonrails.org/getting_started.html#hello-rails.
Leave me a comment if ruby script/server errors out.
I just had the same issue : it was an old mysql gem which was not up to date. Re-installing the mysql gem did the trick.
Re-install mysql-server and mysql-client using this command:
sudo apt-get install mysql-server mysql-client
and then some libraries you need to install to make MySQL available to ruby:
sudo apt-get install libmysql-ruby
This all solved my problem. Try it !!! :)