Rails, Postgres, EC2/AWS: Database Config Does Not Specify Adapter Error - ruby-on-rails

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!

Related

how to connect a postgresql with dbeaver (21.0.0)?

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.

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.

Heroku with AWS RDS: Are details in database.yml needed?

We're running a Rails app on Heroku and have it connected to a database on Amazon RDS. It works fine, the security zone is configured and the app is live.
Heroku requires you to provide a Database URL in the format of
mysql2://user:pass#rdsinstance.com/database
Since we're specifying the DB info in the add-on, what do we need to provide in the database.yml file, if anything?
Would the following suffice, or do we need even less than that? Maybe just the adapter name?
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
Heroku automatically replaces the content of any database.yml file on deploy with the value of the shared database, normally stored in the SHARED_DATABASE_URL config variable.
I don't know if it's save to override that value. If you do it, you should be able to connect to the database from Rails without any additional effort.
If your app is working fine and you are just wondering what you need to write inside the default database.yml file, then you can put in whatever you want, Heroku will replace it in any case on deploy.

Installing Postgres on windows for use with Ruby-on-Rails

Currently I get the following error:
PGError (FATAL: password authentication failed for user "postgres" ):
when my app tries to access the database.
I wanted to test my SQL calls against postgres as my app regular breaks when pushed up to production on Heroku because of the stricter requirements of postgres (which I think is probably a healthy thing) over sqlite 3. So I have a similar request to this for a not out of date tutorial for installing postgres for use with ruby on rails on windows (7) please. I thought it'd be a quick 20 minutes of downloading and installing but 2 and a bit hours later and I don't think I'm very close yet. So far I've:
Downloaded and installed Postgres Version 8.4.8-1 from here
Set my environment variables such that Path (for User) is: C:\Ruby192\bin;C:\Program Files (x86)\PostgreSQL\8.4\bin
Gemfile:
gem 'pg', '0.11.0' # instead of gem 'sqlite3', '1.3.3'
ran bundle install for my rails app seemingly successfully, but haven't found easy way to validate installation yet.
set database.yml as suggested here to:
development:
adapter: postgresql
database: db/development
username: postgres
password: secret
host: localhost
encoding: UTF8
pool: 5
timeout: 5000
I know I need to set up a user name and password for Postgres, maybe also start the postgres server, connect to it(?) and put in my local IP address I'll connect to it on into a config files somewhere and then edit one of the other .conf files in 'C:\Program Files (x86)\PostgreSQL\8.4\data' etc...
I think Rails has made me soft, am I over thinking things or is it actually fairly tricky to set up and I should just go back to Sqlite3, for which there's also the awesomely useful SQLite Manager, Firefox plugin?
I'm still searching for a beginners guide to installing and using Postgres for rails but so far have only been confused by most of the stuff I look at / tried following like this, this, this, this, this(for Snow Leopard), this(linux).
Any pointers would be much appreciated. Thanks!
James
An approach to installing Postgres on windows 7 for use as PostgreSQL database for a rails 3 (3.0.7) project.
Preamble (you can skip this bit)
So the first thing to point out is that Postgres is not just a different file extension from .sqlite3, it's a whole mechanism for managing your databases. As such it has a client/server model, of which you'll need to set up both to use Postgres as the database for your rails app.
Motivation for going through considerable pain of Postgres setup versus almost effortless sqlite setup: if you're deploying to Heroku, they're currently using Postgres so some of your SQL calls that are fine on sqlite3 will break when used with Postgres. It's much easier to debug postgres locally rather than when it's on Heroku's servers.
So I did the following things:
(Disclaimer: I may have forgotten to include some of the things I did... it took me over 48 hours of on and off pain to get it to work... if the following advice doesn't work for you then the huge (2300 pages!!) but very thorough Postgres documentation should help. I'd recommend downloading this anyway if you're serious about using Postgres as it has a lot of material that I've only just begun to understand the significance of.)
(Second disclaimer: I have almost certainly broken 20 sensible Postgres guidelines and exposed security holes in the Postgres database whilst doing so. If there any obvious things an experienced Postgres user disagrees with, please edit my post.)
.Step 1. Download and install PostgreSQL v9.0.4-1 from here because here said only 9.0.x would be supported on windows 7. I kept all the default options and just used 'secret' as the password when prompted by the Postgres installer for one (again not entirely sure what the consequences of sharing that info on the internet is... will soon find out I'm sure). You'll need this password in step 3.
.Step 2. Change environment variables such that Path (for system, not user (I'm not sure if this is significant or not)) is: C:\Program Files\PostgreSQL\9.0\bin
(n.b. I'm on 64-bit windows hence it not being installed for 32-bit in 'C:\Program Files (x86)\PostgreS...')
Don't forget to change access rights to folder PostgreSQL\9.0 and remove any default readonly rights on the folder or content.
(You may also need to restart your computer for these to take effect - thanks #Gavin -although not likely).
.Step 3. Test Postgres installation by trying to create a new database:
From command line: createdb -U postgres mydb_as_postgres.
You should be prompted to enter the password now, if you're not it may be that you need to start the server first (I can't remember whether I needed to do this or not). The easiest way is through pgAdmin III, which should be 'pgAdmin3.exe' in a folder somewhere like C:\Program Files\PostgreSQL\9.0\bin. Once you've started pgAdmin III there should be a panel on the left called 'Object Browser'. In this there should be a tree with:
Server Groups > Servers > PostgreSQL 9.0 (localhost:5432)
Right click on 'PostgreSQL 9.0 (localhost:5432)' and select 'Connect'.
The createdb -U postgres mydb_as_postgres command should create a new databse called 'mydb_as_postgres' which you can check by firing up pgAdmin III and double clicking on 'PostgreSQL 9.0 (localhost:5432)'. Under this there should be:
Databases (2) which should list 2 databases called mydb_as_postgres and postgres
I called it _as_postgres because the -U postgres part of the command tells Postgres to create the database with the postgres user as it's owner, which you need to specify when you're not signed in as the postgres user. I have all of my files stored as 'AJames' user though so if you're the same and want to keep developing your app when signed in as a different user you need to create a Postgres 'role' for that user now (see step 4).
.Step 4. Through pgAdmin III. Right-click on Login Roles (which for me is in):
Object Browser > Server Groups > Servers > PostgreSQL 9.0 (localhost:5432) > Login Roles
Right-click on Login Roles and select 'New Login Role...'
in Role name, put in your operating system user name, which for me is AJames,
and fill in your password under the 'Role Privileges' tab, I checked all the boxes, but an experienced postgres user would likely strongly recommend to only check the 'inherits rights from parent roles' and the 'can create database objects' But I'm not an experienced user and just want to debug Rails SQL calls in Postgres so I also checked the 'Superuser' and 'Can create roles', just in case.
.Step 5. You should now be able to create a new database without being signed in as the postgres user. Try typing:
createdb mydb_as_user
Hopefully this should work for you.
.Step 6. Okay, so you've got a development.sqlite3 file in your rails 'db/' directory. Initially I was going to set the next test as converting this from sqlite3 to psql.
I couldn't get this to work though but I left my attempts here as the solution I used required having the data in a Rails app on Heroku.com (see instead the solution from step 7 onwards). For those who only have a local app and no data in Heroku, they can't use the same approach, so they might need to explore something like this:
x6.1 First, test 'psql' by trying a command from your command line like:
psql mydb_as_user
this should display something like below (after you've typed in your password):
C:>psql mydb_as_user
Password:
psql (9.0.4)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
mydb5=#
x6.2 try entering:
CREATE TABLE users_table (id integer, "name" text);
It should display:
CREATE TABLE
mydb5=#
If you check in pgAdmin III, you should see the table there under:
Object Browser > Server Groups > Servers > PostgreSQL 9.0 (localhost:5432) > Databases > mydb_as_user > Schemas > public > Tables > users_table >
x6.3 Okay, next to try the conversion. Downloaded sqlite-shell precompiled binary for windows.
x6.4 Create a new directory, I used 'C:\temp' and put the sqlite3.exe and your development.sqlite3 files in it.
x6.5 Use the following commands (which are from here) to dump the development.sqlite3 database into Postgres.
sqlite3 development .dump | psql development2
you might get an error like:
psql: FATAL: database "development2" does not exist
x6.6 so I went into pgAdmin III and made a development 2 database, tried the command again and got:
ERROR: syntax error at or near "PRAGMA"
LINE 1: PRAGMA foreign_keys=OFF;
^
BEGIN
COMMIT
Like I said, I couldn't get it to work. I'm sure there's a way of getting round that error but I thought of a different way and so I instead used this solution (which requires a Heroku account to have your data and does the conversion from sqlite3 to psql using the Taps gem (I believe):
.Step 7. in pgAdmin III I created another database. Under the properties tab I set name: 'development', owner: 'AJames' (replace this with your own Windows user name). And under the privileges tab, set role: 'public' and checked the ALL option (thought this resets to unchecked so I'm not sure that's necessary).
.Step 8. add
gem 'pg', '0.11.0'
to your gem file. You'll probably also want to remove the:
gem 'sqlite3'
at this point too.
.Step 9. set database.yml as suggested here to:
development:
adapter: postgresql
database: db/development
username: AJames # replace this with your own user name
password: secret # replace this with your own password
host: localhost
encoding: UTF8
pool: 5
timeout: 5000
If you are working on an open source project and don't want your password to be made publicly available, have a look at some of the answers to Securely providing the database password in a Rails app.
.Step 10. from command line in your rails app's root directory run:
rake db:migrate
This will create the new schema and all the tables in the Postgres database.
.Step 11. run heroku db:pull from your command line (again from in the root directory of your rails app) to pull all your data down and into your new empty Postgres database. I think at this point your taps gem will be doing this work for you.
.Step 12. Hopefully there is no step 12! ...and it should now be working for you. Happy RoR PostgreSQL debugging! Please edit, or let me know, if there are any errors in this.
Also, here's a list of additional stuff that might be interesting/useful:
This is a blog post about Postgres passwords, what they're for,
why you need them, how to change them etc.
This, under 'Creating a spatial database', is useful for newbies to
understand what pg_hba.conf is about and the second link that
Reno gave above, under the 'Using pgAdmin III GUI' is useful to
testing to see if postgres is actually working, before trying to fit
it with Rails (i.e. try creating a database and putting a table and
some data into it).
In the huge but very comprehensive Postgres documentation, I'd
start on page 58, 'I. tutorial'. Then on pdf page 431(!) there's
'Chapter 17. Server setup and operation' that I also found useful.
the answer from AJP is the correct one with just a small re-config.
The line
database: db/development
does not work for me.
I have to change it to
database: development
I use this for ruby on rails for the command
rake db:create
and
rake db:migrate
to work
None of your links appeared to be the (arguably) most useful documentation - the official postgresql docs. I recently configure a Linux Mint box with postgresql and django using a combination of those documents and these, though the latter are specific to Linux.
I'd worry more about validating the postgresql side of things, less about rails. That is to say, your question should be "How can I set up and test a postgresql server on a Windows 7 box", moreso than you need to know how to get it locked into rails.
Edit:
Maybe this could also be of use to you - official postgresql wiki with detailed installation guides.
Good luck!

is there a way to edit herokus yml file

I setup a remote connection locally and need to push it to heroku. When I pushed it to heroku I got an error saying:
RemoteDBName is not configured.
I'm just assuming (also searched and saw) heroku uses their own config.yml file.
Figured this out, for anybody connecting to a remote database on heroku that might see this:
Heroku replaces your database.yml file with their own, overwriting anything in yours.
To get around this:
Create a new file in your config folder, name it whatever.yml
Setup the connection string in this file.
Create a new file in your initializers folder, I called mine load_remote.rb. In this file write this line of code:
REMOTE_DB = YAML.load_file("#{RAILS_ROOT}/config/YOURNEWFILEHERE.yml")
Establish your connection in any of the remote models with this line of code:
establish_connection Remote_DB['Whatever you named your connection string in the yml file here']
Let me show you how database configuration is done when you work with Heroku. I think this might be a bit vague in the documentation, some people get confused over it. Lets utilize the console:
zero:~/Projects/crantastic $ heroku console
Ruby console for crantastic.heroku.com
>> puts File.read(Rails.configuration.database_configuration_file)
---
production:
encoding: unicode
adapter: postgresql
username: something_random
port: 5432
host: somewhere.at.heroku
database: something_random
password: something_random
=> nil
>>
Heroku in practice replaces your apps database.yml when you push your site to their servers. Your data will be stored in one of their fancy PostgreSQL servers no matter what you use locally - this means that you don't have to think about database.yml at all (except for development purpses, naturally). Taps makes sure that everything's db agnostic. If you want to push your latest development db to Heroku, simply run heroku db:push

Resources