I'm trying to migrate users from one system to another. Each system has its own database and different classes.
My plan is to connect to one database, read some info from one database via SQL commands:
ActiveRecord::Base.connection.execute(sql_command)
do something with the data and then write some results on the new database using normal models.
I plan on doing that inside Sidekiq job but I'm trying to do some testing using a Rails console on Heroku.
This should be pretty straightforward, but this proves ridiculously difficult.
When I launched a Rails console on Heroku. I'm connecting to DATABASE_URL, which is ok, but when I try to connect to the old database and execute a command, like this:
ActiveRecord::Base.establish_connection(adapter: "postgresql", encoding: "unicode", pool: 10, url: "postgres://...info...")
ActiveRecord::Base.connection.execute("select count(*) from users")
I end up with:
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 can, however, connect to this old database by launching my rails console on heroku using DATABASE_URL as the env variable:
$ heroku run 'DATABASE_URL=postgres://...info... rails console' -a console' -a app
BUT I don't know how to switch back to my new database so I can update things.
How does one switch databases at run time when using rails console on heroku?
Why try to runtime switch the database? Why not have both connected at the same time and specify at the model level which database they read/write from? Rails supports connecting multiple databases and specifying in individual models what database connection to use: https://guides.rubyonrails.org/active_record_multiple_databases.html
The problem was that using url: didn't work and all parameters needed to be specified.
config = {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "username"=>"u...", "password"=>"p...", "port"=>5432, "database"=>"d...", "host"=>"ec2..."}
If you go for a 3tier database yml, you can use this:
config = ActiveRecord::Base.configurations["production"]["seconddb"]
Then, you can use establish_connection
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("select count(*) from users")
Once I started specifying username, password, port, database and host, it all worked like a charm.
To work with both databases at the same time, a good way is to create a class
class OtherDB < ActiveRecord::Base
establish_connection(ActiveRecord::Base.configurations["production"]["seconddb"])
end
Then you can call things that way
OtherDB.table_name = "table_name"
OtherDB.first
ref (Establish a connection to another database only in a block?)
And to run SQL commands:
OtherDB.connection.execute("select count(*) from users")
Related
i have multiple databases in my project and i want to open rails console to access both databases.
currently i can fetch data of only one default database.
In other word, how to open specific database console?
In other word, how to open specific database console?
From official guide:
bin/rails dbconsole figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL (including MariaDB), PostgreSQL, and SQLite3.
You can also use the alias "db" to invoke the dbconsole: bin/rails db.
If you are using multiple databases, bin/rails dbconsole will
connect to the primary database by default. You can specify which
database to connect to using --database or --db:
bin/rails dbconsole --database=animals
So command is
rails db --db=db_name_from_database_yml
For example you have in your database.yml
production:
my_primary:
adapter: postgresql
database: some_db_name
In this case your command will be
bundle exec rails db --db=my_primary -e=production
Why don't you have a read through the documentation https://guides.rubyonrails.org/active_record_multiple_databases.html.
You should be able to find just what you need. connects_to does the trick of database switching automatically of manually.
Or
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-connected_to
ActiveRecord::Base.connected_to(database: :mydb) do
# Do something here...
end
I am newbie with ubuntu and trying to working with this one. And here is my problem.
I create a new program in rails with database by this command
rails new freelancer --database=postgresql
Everthing woking fine, seems I got a postgresql database with rails.
My problem is, I do not know how to connect my database to the dbeaver program, btw, if I run those command rails db:setup and rails db:migrate after connect my database to dbeave program, it can make some new database ?
And finally can you show me how to start and stop the server too ?
Thank you very much.
Your database connection is specified on your config/database.yml, under your project folder, one block for each environment, but by default you will be using development env.
If you haven't done anything else than what you commented (and you did it right hehe), your database should be already created with name freelancer_development, and you just need to connect to it using DBeaber. Create a new connection using PostgreSQL adapter (check out this video) and following data:
Host: localhost
Port: 5432
Database: freelancer_development
User: [YOUR_POSTGRES_USER]
Password: [YOUR_POSTGRES_PASSWORD]
If you don't know your Postgres user. Try with following combinations, if none of them works, you'll need to setup one.
User
Password
postgres
postgres
postgres
[YOUR_UBUNTU_USER_NAME]
To start your Rails server, just run rails s (short for rails server) on your terminal while being inside your project folder, and hit Ctrl+C to stop it.
Apparently when using the Octopus gem to do Postgres replication everything should be plug and play. However I can't seem to find what I'm doing wrong.
This is my config/shards.yml
octopus:
environments:
- development
replicated: true
development:
slave1:
adapter: postgresql
host: localhost
database: slaveapp_development
username: pguser
password: pgpass
The AR model Provider(I create the exact same tables in each app via Rake tasks) I'd like to sync/replicate to my slave:
class Provider < ActiveRecord::Base
has_many :products
replicated_model()
end
I boot both apps via Rails server and enter Masterapp's console and from there:
> Provider.using(:slave1).create({provider_params...})
#=> works! I get a new record in slave1's DB.
> Provider.using(:master).create({provider_params...})
#=> works partly. Creates record only in master's DB.
The problem is that when calling Provider.using(:master)... I'm expecting:
1 - Create record at master's DB.
2 - Replicate same record at slave1's DB. <--- This is NOT happening.
That is not the purpose of the Octopus gem.
It redirects the database requests, depending on whether it is a read or write operation. That way you can use your Rails models without thinking about the current database connection and if it fits the intended operation.
It does not copy the data to the slaves.
To perform the actual replication, i.e. the data transfer from the master to the slaves, you have to set it up yourself.
There are several options with PostgreSQL. If you are using a newer version (9.1+) you can use the integrated streaming replication in "hot standby" mode. There are tutorials on how to set it up, e.g.
http://www.rassoc.com/gregr/weblog/2013/02/16/zero-to-postgresql-streaming-replication-in-10-mins/
http://prongs.org/blog/postgresql-replication
If you are stuck with an older version of PostgreSQL have a look at the alternatives.
Try adding fully_replicated: false after replicated: true in your config.
I am pretty new to heroku & postgresql. I am trying to dump my production database into my local development database in my local machine.
I dumped like this (my actual info redacted):
pg_dump --host=<myhost> --port=<port> --username=<username> --password --dbname=app_production > output.sql
Then I imported to my local app_development database like this:
psql -d app_development -f output.sql
but now, when I start my server I get this:
PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations : SELECT "schema_migrations".* FROM "schema_migrations"
I also use Navicat to see the local database, and now I can't open any of my tables. Each time I try I get
ERROR: permission denied for relation <nameofwhatevertable>
How can I reset permissions for my app_development database when I've dumped my app_production database.
It seems like your db permissions have been mangled. Do you have PgAdmin III installed? http://www.pgadmin.org/download/
PgAdmin III will let you reset all db and user parameters. It will also allow you to see what is going on in your database(s) and tables so you can debug them.
It's a PostGreSQL development tool that is very much like MySQL Workbench. Free download from PostGreSQL. Runs on multiple platforms. Easy to set up.
It is most handy in cases like this, when you're trying to track down an intractable error. Highly recommended tool. Takes the guessing out of pg work.
I've just deployed my application to heroku and pointed my custom domain to the heroku servers. How can I check the records in my heroku database?
You can use heroku run rails console and look at your records with Model.all or any other method.
If you want to backup the database look at heroku PG backups, you then can import your database on your local machine and look at it there. Depending on your db adapter you could use sqlite browser for sqlite3 or phpmyadmin for MySQL.
I found a similar question like this and here is what #Chowlett says:
"You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table."
You can also type select * from tablename; to view the table contents.
How to view current database schema for Heroku app in Terminal?
heroku db:pull to pull your production DB locally to take a peek in it.
I'll give the method for connecting via a GUI tool
Run the following command to get the database credentials from Heroku:
heroku pg:credentials DATABASE_URL
Then you can use a GUI tool like PG Commander or PGAdmin to connect to the db
Heroku now has an add-on named PostgreSQL Studio (currently free & in beta) that would let you access your database from within the browser, without having to use CLI, much like PHP MyAdmin.
To attach this add-on to your application,
heroku addons:create pgstudio
Then go to the list of add-ons on Heroku, select PostgreSQL Studio, authorize it, select the database to connect with from the dropdown list of all databases and it will take you to the web-based interface to handle your selected database.
You may refer to this official article on Heroku:
https://devcenter.heroku.com/articles/pgstudio
The easy answer is:
heroku pg:info
You can also download a client side Postgres, like Postico, and using the information provided in that URL to enter password and database name etc, then you can create locally, just like phpMyAdmin.
I use the admin_data gem, works well in Heroku.
You can use heroku dataclips that allows to run queries online. Here you can find documentation https://devcenter.heroku.com/articles/dataclips.
Connect to the database using Sequel Pro. You can find your ClearDB url using heroku config command. The structure for connecting is as follows:
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true