Permission denied running ActiveRecord gem tests in PostgreSQL - ruby-on-rails

I'm having trouble running the ActiveRecord gem tests in PostgreSQL and am getting the following error:
rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:1473:in `initialize': could not connect to server: Permission denied (PG::Error)
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I'm able to connect to the server using psql -h localhost and create databases, etc. I'm using http://postgresapp.com/ for the postgresql server. I get the same error when running rake postgresql:build_databases as suggested by the Rails contributing guide.
The rails/activerecord/test/config.yml file has the following settings (all I've changed is the username):
postgresql:
arunit:
username: pete
min_messages: warning
arunit2:
min_messages: warning
username: pete
Is there another setting I need to configure in config.yml? I've tried specifying a host and empty password, but that doesn't help at all.

Your psql is connecting using TCP to localhost, ruby is obviously connecting through a local unix domain socket. Problem with the unix domain socket of postgresql on osx is that not all psql client libraries and database backends agree on the location of the socket.
If you don't pass a -h option to psql it will use a local unix domain socket to. There is a good change it will fail to.
There are two possible solutions for both of which I can't give you details because I do not know ruby on rails and don't have access to a mac right now:
Tell ruby to connect to localhost instead of using the unix domain socket.
Make sure the client library (libpq) used by ruby matches the backend you are running.

Related

heroku pg:psql not working [connection refused]

I am running a Ruby on Rails app on Heroku with Postgres Database.
I used to be able to connect to my database from my terminal using heroku pg:psql command
Recently, things have changed. After running heroku pg:psql command, I get the following error.
--> Connecting to postgresql-perpendicular-94363
psql: could not connect to server: Connection refused
Is the server running on host "ec2-23-23-130-158.compute-1.amazonaws.com" (23.23.130.158) and accepting
TCP/IP connections on port 5432?
I tried to search online about it, but nothing useful has been found.
Please help. Thanks 😄
I've just tried connect to your database:
psql "dbname=postgresql-perpendicular-94363 host=ec2-23-23-130-158.compute-1.amazonaws.com user=test_user port=5432 sslmode=require"
And I've got:
psql: FATAL: password authentication failed for user "test_user"
Because, of course I don't know you credentials :)
However, it seems that connection works. I suppose, that you forgot set sslmode. As you can see in documentation, it is required:
All connections require SSL: sslmode=require.
Update:
I've checked connection without sslmode, and message is other than yours:
FATAL: no pg_hba.conf entry for host "31.42.24.63", user "test_user", database "postgresql-perpendicular-94363", SSL off
So, I suppose that this is a problem with you internet connection. Check you firewall setup.
As per the description mentioned in the post, the way you have specified is correct.
Moreover you can also try the below mentioned command where you specify the database as well.
heroku pg:psql DATABASE_URL
Note: This is only used when there are multiple databases linked.

Creating Users on Postgres - Authentication Issues - Installing

I'm attempting to create a Ruby on Rails web app that uses postgres as its database. I am having trouble connecting to the server with postgres
I have installed PostgreSQL and have successfully created a Rails app that uses postgres. However, when I run "rails server", it returns..
=> Booting WEBrick
=> Rails 3.2.13 application starting in development on XXX:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:1215:in `initialize': could not connect to server: Connection refused (PG::ConnectionBad)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I believe I have an authentication problem. I've tried to follow two SO links, which appear to have worked for a great number of people, but for which the first steps have failed for me.
How to configure postgresql for the first time?
The first step of this link (su root) resulted in: "su: Authentication Failure".
I am trying to use this guide:
https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-12-04
If I use "sudo -i", the "su - postgres" command works.
"createuser" then results in:
Enter name of role to add: XXX(root username)
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
createuser: could not connect to database postgres: 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"?
When trying to follow the Ubuntu Postgres guide at https://help.ubuntu.com/community/PostgreSQL I had less success, with
sudo -u postgres psql postgres
returning:
psql: 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"?
Thanks! I'm still a beginner, so please explain things as simply as possible and elaborate.
You still need to start the postgres server. In ubuntu sudo /etc/init.d/postgresql-8.4 restart

postgresql create user on osx

I'm on osx and I have problems creation of user postgresql.
when I write the command:
createuser myapp -s
I get the following error:
could not connect to database postgres: 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"?
I do not know if it's just PATH directory problem.
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
I installed postgresql with this solution : http://tammersaleh.com/posts/installing-postgresql-for-rails-3-1-on-lion
thanks for your help and sorry for my bad english.
Are you sure the postgres server is running? (Use "ps ax | grep postgres"). Has it opened a TCP port (per default 5432)? Is this unix socket "/var/pgsql_socket/.s.PGSQL.5432" available and do you have the permissions to read/write it?
Furthermore, you may want to check the pg_hba.conf file where the permissions on the postgres server itself are configured.

Postgresql, problems after updating gem

I just updated my gems. And now I have problems connecting to my postgresql database. I get the error:
PGError
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I tried uninstalling the gem and reinstalling, I also tried to change the paths file and put '/usr/local/bin/' on top. I tried some of the things from post:
Repairing Postgresql after upgrading to OSX 10.7 Lion
This app worked fine before updating my gems, other apps still connect just fine, to the same server. I have the same settings in my database.yml file.. what could be wrong?
The error comes from the PostgreSQL server and I have seen it many times. It tells you that you are trying to connect via Unix domain socket (and not via TCP/IP!) to a server that is running locally and listening at port 5432. But no server can be found that would accept connections like that.
You did not mention where the PostgreSQL server resides - I assume you actually mean to connect to a database server on your local machine.
Check your setup, especially your pg_hba.conf file. You need a line like:
local mydb myuser md5
or
local all all peer
or some other connection method that includes your user and database.
These would not help in your case:
host ...
or
hostssl ...
They concern TCP/IP connections, not local connections via UNIX domain socket. When you connect to localhost you actually use TCP/IP via local loop and these settings apply.
Remember to reload after you edit pg_hba.conf. I quote the manual at the linked site:
If you edit the file on an active system, you will need to signal the
postmaster (using pg_ctl reload or kill -HUP) to make it re-read the
file.

Can ActiveRecord connect to PostgreSQL remotely and protect the DB password?

I have a PostgreSQL DB on a remote VPS server (CentOS 5) and I'd like to connect to have a Rails application connect to it from my local Mac laptop. On my laptop, I have the ActiveRecord PostgreSQL adapter installed -- postgres (0.7.9.2008.01.28).
I read in the PostgreSQL docs:
The password-based authentication methods are md5, crypt, and password. These methods operate similarly except for the way that the password is sent across the connection: respectively, MD5-hashed, crypt-encrypted, and clear-text.
[...]
If you are at all concerned about password "sniffing" attacks then md5 is preferred...Plain password should be avoided especially for connections over the open Internet (unless you use SSL, SSH, or another communications security wrapper around the connection).
In a standard Rails database.yml would have something like this for a localhost connection...
development:
adapter: postgresql
database: journalapp_development
username: xxx
password: yyy
host: localhost
But there's nothing in there about the authentication method discussed in the PostgreSQL docs. Is there as option to have something like "auth_method: md5"?
Regardless of whether Postgres allows this functionality, you can enable a secure connection to a remote database by using SSH tunneling. Here's the gratuitous Stack Overflow paste-in from the Web docs:
First make sure that an SSH server is
running properly on the same machine
as the PostgreSQL server and that you
can log in using ssh as some user.
Then you can establish a secure tunnel
with a command like this from the
client machine:
ssh -L 3333:foo.com:5432 joe#foo.com
The first number in the -L argument,
3333, is the port number of your end
of the tunnel; it can be chosen
freely. The second number, 5432, is
the remote end of the tunnel: the port
number your server is using. The name
or IP address between the port numbers
is the host with the database server
you are going to connect to. In order
to connect to the database server
using this tunnel, you connect to port
3333 on the local machine:
psql -h localhost -p 3333 postgres To
the database server it will then look
as though you are really user
joe#foo.com and it will use whatever
authentication procedure was
configured for connections from this
user and host. Note that the server
will not think the connection is
SSL-encrypted, since in fact it is not
encrypted between the SSH server and
the PostgreSQL server. This should not
pose any extra security risk as long
as they are on the same machine.
In case you want more, you can find it online by searching for "SSL tunnel" or "postgres SSL tunnel". Here's the Postgres site where I got the above:
http://www.postgresql.org/docs/current/static/ssh-tunnels.html
To summarize for Rails, you would then do the following:
1) In a terminal window, run the first ssh command above to establish the tunnel.
2) Set your database props like so:
development:
adapter: postgresql
database: journalapp_development
username: xxx
password: yyy
host: localhost
port: 3333
I had a look online and there doesn't seem to be an option for what you're looking for and in fact the client library, libpq doesn't mention this either.
My guess is that this is negotiated on your behalf within libpq. In any case, md5 is likely to be the default authentication method.
If you connect to a PostgreSQL server over insecure channel you need to encrypt your communication with SSL or (as runako has explained) SSH Tunneling.

Resources