I wanted to change from mysql to postgresql.
I don't care about data so I have changed database.yml:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.postgresql
test:
<<: *default
database: db/test.postgresql
production:
<<: *default
database: db/production.postgresql
And run
rails db:reset db:migrate
But I get error
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have restarted both IDE (using c9) and server, but it gave no effect.
Any idea how to solve that problem?
#Edit After running sudo service postgresql start servers starts, and then when I run rails db:create
I get error:
psql: FATAL: role "ubuntu" does not exist
Just run this in your terminal :
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
It's because last time your computer was shut off, Postgresql did not exit gracefully (usually due to running out of battery).
Related
I cannot solve this issue, mysql will not connect. using eb deploy. And I am using Ruby v 2.3.1
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
rake aborted! Mysql2::Error: Can't connect to MySQL server on 'labr.cyp2t1256323.us-west-2.rds.amazonaws.com' (4)
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) (ElasticBeanstalk::ExternalInvocationError)
caused by: ++ /opt/elasticbeanstalk/bin/get-config container -k script_dir
Database.yml
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
database: ***
username: *****
password: *****
host: labr.cyp2t1256323.us-west-2.rds.amazonaws.com
port: 3306
3306 is enabled on Amazon EC2 console, rails-env is at health red
Add a new security group that accept all traffic. And add your db instance to this group.
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html
I got something like this when i hit rake db:migrate
Im using figaro in my app, its my first time with this gem any mysql so I cant get what excatly is wrong.
Thanks in advance :)
Mysql2::Error: No database selected: CREATE TABLE schema_migrations (version varchar(255) PRIMARY KEY) ENGINE=InnoDB
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV["DB_USER"] %>
password: <%= ENV["DB_PASSWORD"] %>
development:
<<: *default
database: <%= ENV["DB_DATABASE_DEV"] %>
test:
<<: *default
database: <%= ENV["DB_DATABSE_TEST"] %>
production:
<<: *default
database: <%= ENV['DB_DATABASE_PRODUCTION'] %>
application.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: password
development:
<<: *default
database: dev
test:
<<: *default
database: test_db
I don't have enough point to ask in comment, but it seems you're trying to run migration without creating a schema first.
Run rails db:create to create the schema first. For example, if you don't have the a database named "dev" in your MySQL, this command will do so.
rails db:create
To set specific environment
rails db:create RAILS_ENV=test
Then you run canrails db:migrate.
To destroy a schema if you need one:
rails db:drop
You're Using ENV
Though it might not be related, have you set up your ENV properly before running the command?
For Windows, you can add those, for example, DB_USER to system environment
Just press Start and type system environment >> Environment Variables... >> New...
Variable Name: DB_USER
Variable Value: your_username
Note: You might have to restart your system for the environment to work.
Or if you don't have time, simply in your terminal you're running rails s:
C:\Sites\ProjectName> set DB_USER=your_username
C:\Sites\ProjectName> set DB_PASSWORD=your_username
C:\Sites\ProjectName> rails s
The same steps for Linux, but I don't know the specific command. Some prefer to use .env file.
I've run into this trouble a couple days ago, and it's very annoying because Rails tried to create dev and test db's when in dev environment. Nothing worked.
I ended up hard-coding database: into database.yml, and let Figaro manage credentials.
I runned the same issue today.
Ensure you have dotenv-rails gem installed and that "DB_DATABASE_DEV" has a value in your .env file.
Also, make sure your database has been created.
It happens when your database.yml does not include database: some_database part or when database name is empty. In your case you can use one of few options:
Option 1: run migrations with database name env variable included
DB_DATABASE_DEV=your_dev_db_name rails db:<task>
Option 2: use hard-coded db name in your database.yml file
# database.yml
development:
<<: *default
database: my_development_database
Option 3: make sure that env variables are correctly loaded
To do so in linux, simply write in terminal:
echo ${DB_DATABASE_DEV}
On windows terminal you need to write this:
echo %DB_DATABASE_DEV%
If you get no content then this env variable is not set
My deployment is running fine until the migration .. which fails
rake aborted!
DEBUG [874287b0] ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
DEBUG [874287b0] /home/kadoudal/rails/swim-tech.eu/site/swimtech/shared/bundle/ruby/2.1.0/gems/activerecord-4.0.3/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection'
I run : cap staging deploy:check:linked_files, whic is successful , database.yml is symlinked correctly and contains :
# encoding: utf-8
defaults: &defaults
adapter: mysql2
host: localhost
timeout: 60000
encoding: utf8
production:
<<: *defaults
database: mydb_production
username: xxxxxxxxxxx
password: xxxxxxxxxxxxxx
why the Adapter is not found ?
forgot I was running a cap staging.... and there is no staging db in the database.yml
I created a staging server by cloning the production server on Rackspace. In this scenario, I want the staging server to still use the 'production' rails environment and the production db (on the staging server), so I set this in the config/deploy/staging.rb file:
set :stage, :production
and it works great.
Can we connect to multiple development instances with a single source and a single database.yml file? I mean, I need to have 4 development instances from same source which connects 4 different MySQL databases. Can I start the servers by using different PORT IDs?
example:
development:
adapter: mysql2
database: TEST1_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-2:
adapter: mysql2
database: TEST2_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-3:
adapter: mysql2
database: TEST3_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
development-4:
adapter: mysql2
database: TEST4_development
host: localhost
username: root
password: password
encoding: utf8
pool: 30
Then, then how we can start 4 different servers (at localhost) connecting these 4 databases simultaneously? (we normally use rails server for single instance in development.)
Thanks :)-
You are not limited to the default rails environments. You could just have development-2, development-3 environments. Read this post on the 37signals blog where DHH talks about that
database.yml can read environment variables.
development:
adapter: postgresql
host: localhost
database: <%= ENV['POSTGRES_DATABASE'] %>
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
then make sure these are set when you start your server.
It is super easy.
Just update your database.yml file as given in the question. Suppose our development instances are development, development-2, development-3 and development-4.
First you just create 3 copies of the environment file development.rb (can find insideconfig/environments/) and rename it to development-2.rb, development-3.rb and development-4.rb.
Then, just do run the below steps before starting the servers.
RAILS_ENV=development rake db:create (OR just rake db:create as it defaults to development)
RAILS_ENV=development-2 rake db:create
RAILS_ENV=development-3 rake db:create
RAILS_ENV=development-4 rake db:create
RAILS_ENV=development rake db:schema:load (OR just rake db:schema:load as it defaults to development)
RAILS_ENV=development-2 rake db:schema:load
RAILS_ENV=development-3 rake db:schema:load
RAILS_ENV=development-4 rake db:schema:load
RAILS_ENV=development rake db:seed (OR just rake db:seed as it defaults to development)
RAILS_ENV=development-2 rake db:seed
RAILS_ENV=development-3 rake db:seed
RAILS_ENV=development-4 rake db:seed
Then, start all instances with different server pid's as given below.
rails s -p 3000 -e development --pid tmp/pids/server.pid (OR rails s by default)
rails s -p 3002 -e development-2 --pid tmp/pids/server.2.pid
rails s -p 3003 -e development-3 --pid tmp/pids/server.3.pid
rails s -p 3004 -e development-4 --pid tmp/pids/server.4.pid
That will start all instances simultaneously from the same source and a single database.yml file. Thank you all for all your replies and help :)-
I'm using Rails 3, and Ubuntu. I want to change the default Rails SQLite3 into a mysql database. I ran these commands:
sudo apt-get install mysql-server mysql-client libmysqlclient15-dev
sudo apt-get install libmysql-ruby1.8
These were successful. Then I did this in the gemfile
#gem 'sqlite3'
gem 'mysql'
This worked, now, what else should I do to complete the MySQL setup. I know for one the database.yml needs to change, but how does it need to look for Ubuntu and how do I fill in the parameters?
Your config/database.yml must look like :
base: &base
adapter: mysql
username: YourUserName
password: YourPassword
host: localhost
encoding: utf8
pool: 5
timeout: 5000
development:
database: YourApp_dev
<<: *base
test:
database: YourApp_test
<<: *base
production:
database: YourApp_prod
<<: *base
Is the database on the same host?
You need to log into mysql, set a user, create a database. If the database is on a database server, outside of the application server, then you will need to make accessible from the net.
http://www.slac.stanford.edu/BFROOT/www/Computing/Online/Databases/CfgDB/CfgDB-MySQL-Setup.html
After you're done setting up the database (not just installing it), then you're database.yml files need to be modified per the above.