I paid someone to create a program with Ruby on Rails where it scrapes data and puts it in a Postgres database. In the program's directory are the standard Rails folders, application, "bin", "config" and other directories.
I'm trying to see a list of the columns in a table. I think the best, or only way, to do this is to log into the actual database, and print it out. I'm trying to use a "psql" command to log in but it is saying:
psql: FATAL: database "dan" does not exist
I'm not sure where the database is, or how I can find it.
This is what the config/database.yml contains:
development:
adapter: postgresql
database: danwork
pool: 5
timeout: 5000
encoding: unicode
username: dan
password: supersecretpassword
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: sports
pool: 5
timeout: 5000
encoding: unicode
username: namename
password: sports_db
Where is my database?
How could I find the database on my own using some linux commands, like find . -iname '...'?
How do I log in, and print out all the columns for the table named "games"?
You want rails dbconsole or rails db.
From the Rails Guide:
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, PostgreSQL, SQLite and SQLite3.
You can also use the alias “db” to invoke the dbconsole: rails db.
To see the contents of your games table:
~/Documents/workspace/<project-dir>$ rails db
psql (9.6.3)
Type "help" for help.
development=# select * from games;
id | date | description | city | state
----+------+-------------+----------------------+-------
1 | | | Boston | MA
2 | | | Seattle | WA
(2 rows)
development=# \q
~/Documents/workspace/<project-dir>$
If you really want to use psql -U <username> <dbname>, you can find these parameters as described in this answer and paraphrased here:
~/Documents/workspace/<project-dir>$ rails c
Loading development environment (Rails 4.1.4)
2.3.1 :001 > Rails.configuration.database_configuration[Rails.env]
=> {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"development"}
2.3.1 :002 > exit
~/Documents/workspace/<project-dir>$ psql development
psql (9.6.3)
Type "help" for help.
development=# \q
~/Documents/workspace/<project-dir>$
As you can see, if no username is returned, you won't need the -U flag.
Your database is running on a server independent of Rails (it will be set up by your coder). It looks like your database is on your localhost.
It will be dependent on your server OS.
If you're looking to show column names in your database, you'll be able to do this in the Rails Console:
$ rails c
$ Game.column_names
To access your database using plain PostgreSQL
su username
psql
\db database_name;
To connect with psql, you must specify the database name.
If you're using password auth, simply psql -U username dbname.
If you're using peer auth, sudo -u username psql dbname.
In your case you seem to be using password auth and connecting to the local host, so I suggest:
psql -U dan danwork
and entering the password when prompted. (You can use a ~/.pgpass file to save the password if desired; see the pgpass documentation)
Related
On my OS X machine, I have a working Rails app (4.2) with Postgres (9.3).
Now, to create a second Rails app, would I need to create a new pg user?
In my first app, no username is provided in database.yml
development:
adapter: postgresql
database: my_first_app_development
pool: 5
timeout: 5000
Short answer no. You can use one user for many databases if that user be owner of this database or has superuser role.
I prefer to use one user in local machine that have superuser privileges.
$> sudo -u postgres psql
postgres# CREATE ROLE my_user CREATEDB SUPERUSER;
postgres# ALTER ROLE my_user WITH LOGIN;
Now you can create database with my_user owner:
postgres# CREATE DATABASE my_database WITH OWNER my_user;
Or just create database with a rake task but before add username to database.yml:
database: my_first_app_development
username: my_user
from shell:
$> bundle exec rake db:create
This good only for local development environment for production create separate user with right privileges.
I am trying to create postgres databases for development and tests. I'm using:
OSX Yosemite
Rails version: 4.2.0
git version: 2.2.2
psql version: 9.4.0
ruby version: 2.1.0p0
HomeBrew version: 0.9.5
Gemfile:
gem 'pg'
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: myapp_development
username: username
password:
test:
<<: *default
database: myapp_test
rake db:create:all returns
PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "myapp_development" ENCODING = 'unicode'
.... (lots of tracing)
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_development", "username"=>"username", "password"=>nil}
myapp_test already exists
What is wrong?
EDIT
I just tried changing the username in the database.yml to my username that I'm using on my Mac. It worked. It also told me that not only maybe_test already exists, but it also just told me that myapp_development already exists too.
Why wouldn't it be able to use the other username that I had created and assigned a role to CREATEDB?
Why did it say that the development couldn't be created then tell me that it already existed?
This all seems way too confusing and reminds me of PHP setup with Apache back in the very old days. I don't want to have to deal with problems every time I create a new app and try to follow the Heroku recommendations to use PostgreSQL during development too.
I have faced same issues when running rake db:test:prepare in postgresql on my Ruby on Rails project. This is pretty clear from the error message, that its a permission issue for the user. I added CREATEDB permission for new_user as following from the console.
To access postgres console:
$ sudo -u postgres -i
postgres#host:~$ psql
In there:
postgres=# ALTER USER new_user CREATEDB;
It's working perfect for now. You may have another issues with database ownership, for this you can change database privileges and owner as following command.
postgres=# GRANT ALL PRIVILEGES ON DATABASE database_name to new_user;
postgres=# ALTER DATABASE database_name owner to new_user;
Looking at your schema your credentials for development and test are different.
Perhaps remove username and password from the schema, seeing that your test database did get created.
create database demo;
create user demotest with password '123';
grant all privileges on database demo to demotest;
commit;
This is script for creation of database. But any existing database having password '123' then change your password for new database to password '1234'. This procedure working for me.
I am having trouble dropping my postgres database for a rails app - switched recently from sqlite after deploying to heroku. When running my rake task to aggregate data and populate the database, rake db:create db:migrate all works, but db:drop is not working. I am getting the following error in my terminal after running rake db:drop db:migrate db:create all. 'all' is a rake task for an aggregator.
Couldn't drop something_development : #<PG::Error: FATAL: database "postgres" does not exist
>
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"?
>
something_development already exists
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"?
'This is a look at my database.yml file...`
development:
adapter: postgresql
host: localhost
database: something_development
username: username
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: username
database: something_production
Also here is a list of databases from psql command line:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+-----------------+----------+---------+-------+-------------------------------------
username | username | UTF8 | en_US | en_US |
something_development | username | UTF8 | en_US | en_US |
template0 | username | UTF8 | en_US | en_US | =c/username+ | | | | | username=CTc/username
template1 | username | UTF8 | en_US | en_US | =c/username + | | | | | username=CTc/username
(4 rows)
EDIT - I am trying to do this in development before pushing to heroku. Do i need to add access privileges to my username? I tried GRANT UPDATE ON something_development TO username but received ERROR: relation "something_development" does not exist. Will remember that for Heroku - thanks.
Does anyone have any ideas on what might be happening or some resources that might point me in the right direction? Appreciate any help - thanks.
At first, you should make sure that which kind of postgres DB using on Heroku: Shared DB or Heroku Postgres.
If you are using Shared DB (it's recommended that shouldn't use any more), unfortunately you have to remove your Heroku app and re-create new one with the same app name.
If you are using Heroku PG, you could find your DB here: https://postgres.heroku.com/databases and just destroy and add another DB, it's like drop and re-create your DB, after that you can run migration as well. With Heroku PG, I'm not sure whether we can run db:drop now (It's been a while since I was fail to use this command on Heroku)
About configuration you should follow few steps below:
install gem pg
using pgAdmin III or something like that to try to connect to your PG server. If you're successful, use this information for your database.yml file.
Notice: When you push your code to Heroku, Heroku will automatically create new database.yml basing on their PG server information, so you don't need to care about configuration database on Heroku. Remember removing sqlite gem before pushing your app to Heroku
On heroku you don't have privileges for drop database, you have to do
heroku pg:reset
If your database is not on heroku, check the user (of postrgesql) has privileges.
Figured out the problem - turns out you cannot rename the database "postgres" because it needs that to manage the database. I had tried renaming postgres to something_development. To fix this, I created a postgres database.
I am trying to prompt the psql interface to try to create a database, actually following from Dr. Hartl's tutorials http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=3.2.
I created the project with:
rails new postgr_ --database=postgresql
I added passwords to the database.yml file:
development:
adapter: postgresql
encoding: unicode
database: postgr__development
pool: 5
username: postgr_
password: 12345
test:
adapter: postgresql
encoding: unicode
database: postgr__test
pool: 5
username: postgr_
password: 12345
production:
adapter: postgresql
encoding: unicode
database: postgr__production
pool: 5
username: postgr_
password: 12345
I then enter into terminal:
$ rails db
And I get the following error after entering my password:
psql: FATAL: password authentication failed for user "postgr_"
I've been going at this a good part of yesterday and all day today and was unable to work around this. I may very well be missing something fundamental, if you spot it please let me know. Thank you!
You're missing the steps to setup the postgresql role and database creation.
This procedure depends on the system you are using. I will assume that you are using a mainstream linux distribution.
First login to the postgresql account. You may use one of the following commands:
$ su - postgres
or
$ sudo -i -u postgres
Once logged in, start the psql program:
$ psql template1
At the psql prompt, create a new user role and a database for your project:
=> create role postgr_ with createdb login password '12345';
Then simply quit the program
=> \q
And logout from the postgresql user account
$ exit
Then you should be able to run the rail db command successfully
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