fastmonkeys / stellar

Fast database snapshot and restore tool for development
MIT License
3.86k stars 119 forks source link

Fails to connect to psql #28

Closed jayzeng closed 9 years ago

jayzeng commented 10 years ago

Feel bad to ask this question, but I am having trouble of connecting to psql, here are the steps:

(env)➜  shell  stellar init
Please enter the url for your database.

For example:
PostreSQL: postgresql://localhost:5432/
MySQL: mysql+pymysql://root@localhost/: postgresql://localhost:5432/api
FATAL:  database "api/" does not exist
Could not connect to database: postgresql://localhost:5432/api/
Make sure database process is running and try again.

Please enter the url for your database.

On the other hand, I can manually connect to this api.

➜  api git:(mapping) psql --host localhost --port 5432 -d api
Null display is "[NULL]".
Expanded display is used automatically.
Timing is on.
psql (9.3.5)
Type "help" for help.

Did I miss anything?

nobuf commented 10 years ago

I've faced the same error. After reading sqlalchemy's document a little I tried to pass postgresql+psycopg2://ubuntu:@/testdb?host=/var/run/postgresql, but it asks me my db name again Please enter the name of the database (eg. projectdb): and returns another error if I type the db name:

Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/testdb/.s.PGSQL.5432"?`

The socket file should be at /var/run/postgresql/.s.PGSQL.5432.

database_exists and other methods in stellar's code do not seem supporting additional params like specifying unix socket. After created a user with alter user newuser createdb and allowed tcp access in pg_hba.conf, postgresql+psycopg2://newuser:newpass@localhost:5432/ is working in my case.

ramonakira commented 10 years ago

I'm not sure if this is related, but this worked for me: postgresql://postgres@127.0.0.1:5432/

e.g. add the username of the database and use the IP instead of hostname localhost.

jayzeng commented 10 years ago

@ramonakira I tried what you suggested with no luck. Got the exact same error.

joshma commented 10 years ago

Has anyone else gotten this to work? I've tried a variety of URLs, including the full one postgresql://joshma@localhost:5432/mydb and it won't work, while psql -h localhost -p 5432 -U joshma mydb works fine.

zosiu commented 10 years ago

@nobuf :+1: postgresql+psycopg2://newuser:newpass@localhost:5432/ has worked for me too

pypingou commented 10 years ago

The root of this problem is: https://github.com/fastmonkeys/stellar/blob/master/stellar/command.py#L160

If you print the OperationalError exception what you get most of the time is database does not exists and that's due to the trailing slash that's added in the URL that contains the database name.

If the URL does not contain the database name, then it's like sqlalchemy tries to find the database having the same name as the user, so if you do not have a database named <user>, it fails to connect as well.

I'll see if I can come up with a patch that keeps the original idea of the author while allowing to connect to a specific database.

joshma commented 10 years ago

@pypingou thanks for the patch, that worked for me

vendion commented 10 years ago

I am also having problems getting it to work with a database that requires authentication to work. I have tried various formats:

postgresql://user:pass@localhost:5432/
postgresql://user:pass@localhast:5432/dbname
postgresql+psycopg2://user:pass@localhost:5432/
postgresql+psycopg2://user:pass@localhost:5432/dbname

No matter what though I get the same error:

Could not connect to database: postgresql+psycopg2://user:pass@localhost:5432/dbname
Make sure database process is running and try again.

Please enter the url for your database.

The server is running and I am able to connect to it with the psql command, and my app connects to it fine as well.

glennmartinez commented 10 years ago

Can someone tell me what's going on here?

 lects/postgresql/psycopg2.py", line 422, in dbapi
 import psycopg2
  File "/Library/Python/2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
  from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
 ImportError: dlopen(/Library/Python/2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded:       libssl.1.0.0.dylib
  Referenced from: /Library/Python/2.7/site-packages/psycopg2/_psycopg.so
 Reason: image not found
pypingou commented 10 years ago

@vendion see #31

@glennmartinez you seem to be missing some libraries: libssl perhaps?

Teemu commented 9 years ago

This was fixed by @pypingou's PR (thanks!).