I am on a Ubuntu 14.04 LTS machine.
I found useful information on how to configure PostgreSQL in Ubuntu for Rails development at help.ubuntu.com, at Heroku and at digitalocean.com.
Putting everything together, all the information seems to converge on the necessity of creating a database superuser with login name that match my Ubuntu user name with: sudo -u postgres createuser --superuser $USER
When time arrives to create a password for the new superuser with sudo -u postgres psql , I am wondering if Rails can use PostgreSQL without setting the password, if this password can and should be different from my Ubuntu account password and also whether database.yml could be a security concern when pushing to Git repository hosting web sites and to Heroku. Indatabase.yml in fact is recorded exactly this kind of sensitive information.
According to Heroku it is necessary "to export the DATABASE_URL environment variable for your app to connect to it when running locally", with: export DATABASE_URL=postgres:///$(whoami)
Is that really necessary? At help.ubuntu.com and digitalocean.com this information is not reported.
Finally I am wondering whether the choice of installing PostgreSQL through the PostgreSQL apt repository would be safe enough or it would be preferable to install the LTS version of Ubuntu.
There are two ways for Rails to set the connection with a database: via config/database.yml or via the environment variable ENV['DATABASE_URL']. See at guides.rubyonrails.org
By default $DATABASE_URL is empty:
echo $DATABASE_URL
If posgresql is installed via PostgreSQL apt repository, in order for Rails to use the pg gem it is also necessary to install the libpq-dev package, otherwise bundle install will fail. See Can't find the 'libpq-fe.h header when trying to install pg gem.
From 'man createuser':
createuser creates a new PostgreSQL user (or more precisely, a role). Only superusers and users with
CREATEROLE privilege can create new users, so createuser must be invoked by someone who can connect as a
superuser or a user with CREATEROLE privilege.
When postgresql is installed, it creates a user postgres with role postgres. It also creates a postgres system account.
So this is why createuser should be run as postgres user in order to connect to postgresql for the first time and add the user $USER (current system user).
It is not necessary to create a password for the new database user. Most people add database.yml to their .gitignore file so it stays out of version control. It is also possible to use .pgpass to keep sensitive
information out of the *.yml file: see postgresql documentation.
It is possible to connect to postgresql only as a database user AND through an existing database.
During installation from the postgresql apt repository, postgresql only creates the postgres user and the postgres database.
The psql command allows the current user to connect to the postgresql database named after the current user. So, if the system user is 'dave' AND there is a 'dave' database it is possible for 'dave' to connect to the 'dave' database with command psql with no options.
If 'dave' is a database user but the database 'dave' was not created, for dave to connect to postgresql it is necessary to specify an existing database with:
psql -d postgres
Alternatively, it is possible for dave to connect to postgresql executing the psql command as the postgres user with sudo:
sudo -u postgres psql
Related
I was using sqlite but switched to pg for some reasons.
I included pg gem in the makefile and made changes in the config/environments.
When I started migrating the data using rake db:migrate , I’m getting this error.
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've gone through all the posts related to this but did not find a solution.
I'm using ruby 2.1.5 and rails 4.2.0.
sudo /etc/init.d/postgresql start
or
sudo /etc/init.d/postgresql restart
Both should work just fine
If you still get an error you should fix the config/database.yml file one way to go about this is simply create a new project
rails new yourapp -d postgresql
then just copy the database.yml file
if you need to create an new user and password
sudo -u postgres createuser john -s
If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password john
Most of this is from gorails
You need to start your PG server:
$ postgres -D /usr/local/pgsql/data
http://www.postgresql.org/docs/9.1/static/server-start.html
I'm working on an application in Rails for my college. The application was started by the students from previous year and now it's me and my colleagues turn to continue work on it. I took the application from github, I run bundle install, but when to run rake db:migrate I got this PG::ConnectionBad: FATAL: password authentication failed for user "alphauser".
In database.yml I have these
development:
adapter: postgresql
encoding: unicode
database: alpha_database
host: localhost
pool: 5
username: alphauser
password: alphapassword
I don't know what to do in this case.
You have to create corresponding user and database manually like this:
in shell:
psql
then:
create user alphauser with password 'alphapassword';
create database alpha_database owner alphauser;
alter user alphauser superuser createrole createdb replication;
\q
don't forget semicolons.
Try to use:
sudo -u postgres createuser --interactive --pwprompt
to add the role and the password
So the problem is with your rails application using the database.yml file to try and connect to your local database and failing to find the user alphauser (since I assume you're using different computers/environments as the previous students).
Databases have users similar to applications, the postgres documentation is pretty dense on this, but I would guess if you can create a user alphauser, and make it's password alphapassword then you will have a new clean database for your app that you can run rake db:migrate on.
Postgres docs: http://www.postgresql.org/docs/9.2/static/app-createuser.html
Try running this command from the command line createuser -P -s -e alphauser
This will prompt for a password, which is alphauserpassword
create user alphauser with password 'alphapassword';
Then
rake db:setup
It's taken me two days to get this far. But I'm almost there. When I run cap deploy:cold the deployment fails with this message:
servers: ["xxx"]
[xxx] executing command
** [out :: xxx] rake aborted!
** [out :: xxx] FATAL: password authentication failed for user "my_app"
My database.yaml has this:
production:
adapter: postgresql
encoding: utf8
database: my_app_production
pool: 5
host: localhost
username: my_app
password: secret
I'm running cap deploy under a user I created called deployer. I installed Postgres under deployer. I also created the user my_app in psql:
create user my_app with password 'secret';
create database my_app_production owner my_app;
I verified that the user my_app exists by running \du. When I ssh through deployer#xxx and I run the command psql I get psql: FATAL: role "deployer" does not exist.
What am I doing wrong?
For the psql invocation to be similar as what rails does, it should be:
psql -h localhost -U my_app -d my_app_production
If you omit these options, psql will take by default:
the OS username as the database username
the OS username as the database name
a Unix socket directory as the host (which differs from localhost and generally has different authentication rules in pg_hba.conf)
I've gone through the very same Railscasts you are going through I think. I just got my site up with it, though I am still having a couple glitches with cap deploy. In any case, for clarity.
You created a deployer user on the remote host obviously. But, that doesn't mean "deployer" is a user in Postgres. Once you ssh as deployer#xxx.xxx.xxx.xxx you have to run sudo -u postgres psql to enter psql as the default postgres admin. Presuming you're using Ubuntu, there is no default password.
Where I'm confused is how, if you can't get in on the remote system with deployer, did you create the my_app user in psql there? It almost sounds like you did this locally (Vagrant maybe?) and didn't do it remotely. As I said, I think I just finished the same stuff you're looking at and it works if the my_app user is created on the remote box with the correct password.
I need to convert an app from sqlite to Postgresql in order to use search with thinkingsphinx. I have run the following steps what am I missing?
I added the pg gem to my gemfile, ran brew install postgresql, and have configured my database.yml file as follows:
development:
adapter: postgresql
database: example_development
username:
password:
host: localhost
encoding: UTF8
I have also run the commands that homebrew suggests on installation:
If this is your first install, create a database with:
initdb /usr/local/var/postgres
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.0.4/org.postgresql.postgres.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
What do I put for my username and password? Is there a file I need to edit or do I need to create a database from the command line? There must be something simple that I am missing. I haven't run any custom sql queries, and have stuck to active record defaults.
You need to specify the username and password of the account on the Postgres database you are using.
If you do not have a local postgres installation running, then there is no database to connect to, so you will have to follow some instructions online that outline installing and setting up a postgres database on your development machine.
I'm attempting to get my first "hello world" rails example going using the rails' getting started guide on my OSX 10.6.3 box.
When I go to execute the first rake db:create command (I'm using mysql) I get:
simon#/Users/simon/source/rails/blog/config: rake db:create (in /Users/simon/source/rails/blog) Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"blog_development", "pool"=>5, "password"=>nil, "socket"=>"/opt/local/var/run/mysql5/mysqld.sock"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I found plenty of stackoverflow questions addressing this problem with the following advice:
Verify that user and password are correct (I'm running w/ no password for root on my dev box)
Verify that the socket is correct - I can cat the socket, so I assume it's correct
Verify that the user can create a DB (As you can see root can connect and create a this DB no problem)
simon#/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blog_development;
Query OK, 1 row affected (0.00 sec)
to ensure that this wasn't a charset issue I also tried:
mysql> create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
Note: here is my database.yaml:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password:
socket: /opt/local/var/run/mysql5/mysqld.sock
# host: localhost
Note that I tried switching socket to localhost with no effect.
Any idea on what might be going on here?
Thanks for all the help guys. Looks like the problem had to do with my install of the mysql gem under OSX.
#tim after I proceeded to the next step and got up and going I got an error on the console, so I did a bit of searching and found this helpful thread.
After I uninstalled my ruby gems gem uninstall mysql I installed the proper mysql gems using this command (from the thread):
export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
After executing this one I was able to successfully run rake db:create and proceed.
Thanks!!
It could be a number of things.
Is your database set for utf8 character set?
Is the path to the socket correct, since it varies from OS.
Have you reinstalled the mysql gem? sudo gem install mysql
It might be MySQL, you might want to downgrade the version to 5.0
Other than that, I'm not sure.
You should post here your database.yml
To make your test better, i would try to create a database UTF-8 to see if your database supports utf-8
create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci
Does the blog_development database already exist?
If so, you can just continue to the next step.
Try running ruby script/server in the blog/ directory.
If it doesn't error out, then you should be able to navigate to localhost:3000 and continue the tutorial from here http://guides.rubyonrails.org/getting_started.html#hello-rails.
Leave me a comment if ruby script/server errors out.
I just had the same issue : it was an old mysql gem which was not up to date. Re-installing the mysql gem did the trick.
Re-install mysql-server and mysql-client using this command:
sudo apt-get install mysql-server mysql-client
and then some libraries you need to install to make MySQL available to ruby:
sudo apt-get install libmysql-ruby
This all solved my problem. Try it !!! :)