How to migrate a postgresql database - ruby-on-rails

We had a developer developed a website under his local Mac, and the developer just transferred the zip file of rails site to me, but I am using Windows.
Could anyone help me over the steps on how to change from his database to my local postgresql database ? Sorry I am new at this.
So far:
1) I've edited the database.yml file, host is localhost port 5432 and database name is data1, so I went into pGAdminII and create the same database name.
2) I've created a super user and supplied the credential to database.yml.
Is that all I need to do ? Do I need to setup anything else ? thanks!

You'll need to change the adapter in the config/database.yaml file from whatever it was to postgres, something along the lines of:
development:
adapter: postgresql
database: data1
host: localhost
username: someone
password: something
You will also need to ensure you have the relevant gem in your gemfile.
gem 'pg'
That should just about do the trick. Run your rake db:version to test the connection, if all seems well go for a rake db:migrate. Post any errors you get.

Related

Rails: fe_sendauth: no password supplied (PG::ConnectionBad) from Ruby, but ok in Rails

I'm having problems assessing a postgres database from straight ruby.
I've created a Postgres database using Rails
>rails new www --database=postgresql
using Rails 4.2.5 and Postgres is 9.4
It produces the following config/database.yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: www_development
test:
<<: *default
database: www_test
production:
<<: *default
database: www_production
username: www
password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
I can run rails server, db:drop, db:create and db:migrate fine.
I can also access the database fine with psql
>psql www_development
But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.
It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' pg_hba.conf file.
At wits end trying to figure this one out. Any help would be much appreciated.
app.rb
require 'yaml'
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?
as per documentation peer auth does
Obtain the client's operating system user name from the operating
system and check if it matches the requested database user name.
That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...
Options:
put username: postgres to ActiveRecord::Base.establish_connection(
change local all all peer to local all all trust
With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.
development:
<<: *default
database: postgres
username: postgres
password: postgres
# must specify the right DB, superuser and superuser Password as per postgreSQL setup
This worked for me, the only changes I needed to make. (ok fine i re-installed pgsql with 12.1)
I had this same issue when setting up a Rails 6 application.
The issue was that when start the rails server using rails server, and try to view the application from a browser, I get the error below:
ActiveRecord::ConnectionNotEstablished
fe_sendauth: no password supplied
Here's how I solved it:
The issue was caused by me not specifying/supplying the database password to be used by the application, so when the application tries to connect to the database specified in the config/database.yml it runs into that error.
I solved it by specifying the database password for the application in the config/database.yml:
# The password associated with the postgres role (username).
password: my-password
Also, ensure that you've created the database for the application if you've not created it already using:
rails db:create
Afterwhich I restarted the rails server:
rails server
This time when I tried viewing the application from the browser, it worked fine.
That's all.
I hope this helps
I was trying to use peer authentication which shouldn't ask for the password.
For me the issue was that I was specifying localhost in the database.yml so that was forcing the database driver to try to make a different type of connection (e.g., tcp, named pipe, etc)
Removing the "host: 'localhost'" line from my config solved the problem for me per this other answer: ActiveRecord connection to a localhost postgresql database in trust mode
Note that in my case I'm using the 'peer' method and not the 'trust' method but the solution is the same

database.yml file configuration and postgres - rake db:drop db:create db:migrate

I recently switched to postgres for a rails app in development since I deployed on Heroku and want to match production. Postgres is installed and working correctly for my application when I set the database name equal to 'postgres', which is the default name from what I understand.
However, I would like to change the database name for development, test and production. If I simply change the names in database.yml, and try to run a rake task, I get the following error in my terminal:
NOTICE: database "something_development" does not exist, skipping
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?>
rake aborted!
FATAL: database "something_development" does not exist
Here is what my database.yml file currently looks:
development:
adapter: postgresql
host: localhost
database: something_development
username: name
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: name
database: something_production
Are there some other steps I am missing? Apologies if I am missing something obvious here! Thanks!
Changing the name in the database.yml file only changes the place that it looks for the database, it does not actually rename the database itself. If you change the specified location, you will need to rename the database to match, using a GUI tool (like navicat or pgadmin) or the command line.
Also, it is probably an obfuscation omission, but your posted output refers to both shredset_development and something_development. If this continues to fail, perhaps verify that all of your names are the same everywhere...

Ruby on Rails server stopped working

I am new in ror developement..i was working on a LIVE server...I just uploaded a file through sftp...after 1 day server suddenly stopped working...You can see the error message from here
it shows
There appears to be a database problem.
Your config/database.yml may not be written correctly. Please check it and fix any errors.
Your database schema may be out of date or nonexistant. Please run rake db:migrate to ensure that the database schema is up-to-date.
The database server may not be running. Please check whether it's running, and start it if it isn't.
Looking at the error page you seem to be using Rails 2.3?
At a guess you have a MySQL database not an SQLite running. You should have the user name and password for the database around somewhere (replace the relevant fields in the 3 sections with them).
Change the database names to reflect your database names.
The server admins might have set a specific socket for MySQL in which case replace the '/tmp/mysql.sock' with the socket number.
Check your Gems to see if the MySQL adapter is installed (you appear to be using Rails 2.3 so try gem list on the terminal for your server - make sure that you are in the root directory for the app).
If the MySQL gem is missing use gem install to install it (this will depend on what your hosting provider allows).
The following links are pretty old - targetted towards Rails 2 which you appear to be using.
http://www.ruby-forum.com/topic/139710
http://forums.mysql.com/read.php?116,353922,359544
database.yml
development:
adapter: mysql
encoding: utf8
database: temp_development
username: root
password:
socket: /tmp/mysql.sock
# 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: mysql
encoding: utf8
database: temp_test
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
database: temp_production
username: root
password:
socket: /tmp/mysql.sock

rails 3 postgreSQL basic 'database does not exist'

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

ROR change application database from SQLite to PostgreSQL

I have a web application which uses SQLite. I deploy it on heroku which uses PostgreSLQ. This causes problems sometimes and I was advised to develop my app using PostgreSQL instead of SQLite.
I found out that I should modify database.yml like that (same for test and production):
development:
adapter: postgresql
database: my_database
username: my_username
password: my_passwod
host: /var/run/postgresql or localhost
Well the only database I've ever used is SQLite, so I just tried to take my chances, but failed. I filled this file with some random data.
rake db:migrate resulted in:
When I used host: localhost
> could not connect to server: Connection refused Is the server running
> on host "localhost" and accepting TCP/IP connections on port 5432?
When host: /var/run/postgresql
> 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 suppose I should start PostgreSQL server first, but have no idea how to do this. Please give me a step by step answer how to move from a SQLite application to a working PostgreSQL application.
I would like to advise to you that you should download Postgresql including the PGADMIN itself which is easier to use than the psql terminal.
And I think when you download/install Postgresql from their official website... the package was complete already.
Upon installing, the postgresql will ask you a certain password that you will be using in accessing your postgresql server.
After the installation, open the PGADMIN and connect to the server. Enter your password (which you had declared during installation).
If you can't connect to the server, then edit the port. To do this, right click the server then go to properties... edit the port into something which is free. Example: 5433 and so on. It's up to you.
If everything's finally working... setup the correct config for your database.yml
This is important:
development:
adapter: postgresql
database: name_of_database_here
host: localhost
username: postgres
password: your_db_server_password_here
pool: 5
timeout: 5000
port: 5433
Okay from that config info above, specify the important parts. By default, your db server username is postgres and obviously your host is localhost because you are setting up under the development.
If your port is 5432 by default then just remove the port part.
Let's go to your gemfile.
In order for you to deploy your app in heroku. Use gem 'pg' instead of sqlite3.
If you have an existing sqlite3 database then put the gem inside the development group. In that case, Heroku will successfully bundle during git push heroku master process.
group :development do
gem 'sqlite3'
end
Your gem 'pg' can either go outside the groups or put it in your production group.
Important:
Before any deployment procedure, make sure that you can run the app locally (localhost). Then if everything's working... that's the time that you should organize the necessary stuffs appropriately.
If you wish to switch to Postgresql instead of sqlite3 after pushing the app to Heroku... you can do so by pgbackups add-on and pg_restore the dump file into your local postgresql db server.
That's it. Hope it helps.
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Resources