Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent postgrest from starting if DB is not available #682

Closed
michalskop opened this issue Jul 25, 2016 · 11 comments
Closed

Prevent postgrest from starting if DB is not available #682

michalskop opened this issue Jul 25, 2016 · 11 comments
Labels
enhancement a feature, ready for implementation

Comments

@michalskop
Copy link
Contributor

michalskop commented Jul 25, 2016

I may be missing something, but I can (with Content-Type: application/json) :

POST /animal 

with multiple data

[{"name": "cat"},{"name": "dog"}]

(returning correctly 201)

But I cannot do the same POST with just a single item

{"name": "cat"}

nor with

[{"name": "cat"}]

Both returning 500 Internal Server Error with Something went wrong and with headers:
Connection →close
Content-Type →text/plain; charset=utf-8
Date →Mon, 25 Jul 2016 21:11:01 GMT
Server →postgrest/0.3.2.0
Transfer-Encoding →chunked

(running Postgres 9.5 on Ubuntu 14.04, tried in Postman and using PHP)

There is no information neither in Postgres log nor in syslog.

Any idea what may be wrong?

@pmahoney
Copy link

I had this same issue, strangely enough. The details of my specific case are possibly a bit odd, but posting this for posterity.

I was running both PostgreSQL and PostgREST as docker containers on the same host. The networking mode was set to use (I think), Rancher's overlay networking even though this had not been enabled in this Rancher instance (running an old version of rancher-agent, 0.8.2).

Bizarrely, POST with multiple elements in an array did succeed and did create new records in the database. But POST with single element resulted in exactly the above error ("Something went wrong").

Also, POSTing an empty body to a non-existent URL failed with the same error, e.g.

$ curl -i -X POST -d '' http://localhost:3000/no-such-endpoint
HTTP/1.0 500 Internal Server Error
Date: Mon, 22 Aug 2016 16:21:48 GMT
Server: postgrest/0.3.2.0
Content-Type: text/plain; charset=utf-8

Something went wrong

Though a GET fails with the expected 404:

curl -i http://localhost:3000/no-such-endpoint
HTTP/1.1 404 Not Found
Transfer-Encoding: chunked
Date: Mon, 22 Aug 2016 16:21:30 GMT
Server: postgrest/0.3.2.0
Content-Type: application/json

The fix in my case was to switch to using the (default in docker) "bridge" networking. I also ran both PostgreSQL and PostgREST in the same network namespace, allowing them to communicate over "localhost". So I'm not completely sure which of the two changes solved it, and I have no hypothesis for what exactly would cause the observed behavior.

Would it be possible to provide a better error message in this (truly bizarre) case?

Thanks

@begriffs
Copy link
Member

begriffs commented Aug 22, 2016

Thanks for providing more info to reproduce the error. It's hard to know the exact place in the code which causes this error. I can make a branch for you to run which uses exceptionResponseForDebug. It would include exception details in the server response.

This also might be a good time to revisit #540 to enable customizing the verbosity of server logs with a flag.

@pmahoney
Copy link

@begriffs Sure, if you make a branch, I will try to reproduce and hopefully get some more clues.

@michalskop
Copy link
Contributor Author

I run into the same problem on second server. The really weird thing is that it used to work ok some 3 weeks ago, but it is not anymore and it allows POSTing only multiple items (I have scripts I run at that time, they do not work anymore).
(Even after reboot.)
If there is anything I may try to debug this, let me know.

@michalskop
Copy link
Contributor Author

michalskop commented Aug 25, 2016

@begriffs
I have created a sample table to provide test for this issue:

POST https://api.hlasovali.cz/test

with a single header
Content-Type: application/json

A) not working (returning 500 Internal Server Error - Something went wrong)

{
    "number": 1,
    "text": "test 1"
}

B) working ok (201)

[
    {
        "number": 1,
        "text": "test 1"
    }, {
        "number": 2,
        "text": "test 2"
    }
]

(The table test itself is created as (using example basic_auth):

CREATE TABLE public.test
(
  "number" bigint NOT NULL,
  "text" text
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.test
  OWNER TO postgres;
GRANT ALL ON TABLE public.test TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.test TO author;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.test TO anon;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE public.test TO admin;

)

(Any idea what can be wrong /how to solve this/ would be greatly appreciated as my app https://hlasovali.cz stopped working for inserting new data because of this issue.)

@ruslantalpa
Copy link
Contributor

@michalskop sending a single item is not supported (nowhere in the docs does it say this is the way to do it) however this was implemented in a recent PR that is merged #634

you'll need to compile it yourself or wait for a release

@michalskop
Copy link
Contributor Author

michalskop commented Aug 25, 2016

@ruslantalpa thanks!

However it does say so in the documentation here http://postgrest.com/api/writing/, the very first example:
POST /table_name

{ "col1": "value1", "col2": "value2" }

But it could be overcome by always posting as an array, but neither this works:
POST https://api.hlasovali.cz/test

C) not working (500 - something went wrong)

[
    {
        "number": 1,
        "text": "test 1"
    }
]

Therefore there is no way how to insert a single item.
(And again, the strange thing is it definitely worked a few weeks ago in this exact installation.)

@ruslantalpa
Copy link
Contributor

jumped to conclusion ... sorry. will look into it a bit later

@ruslantalpa
Copy link
Contributor

I see your url is https, which means it goes through a proxy. Can you try postig directly to postgrest? Go to our gitter channel and i'll help you debug

@michalskop
Copy link
Contributor Author

Note: so it seems working ok if started Postgrest manually or restarting manually the service (sudo service postgrest restart), but it does not work if postgrest is started automatically on reboot (using /etc/init/postgrest.conf)
So it is important to make sure postgrest starts after the db is up.

@begriffs begriffs changed the title Cannot POST a single item (but multiple yes) Prevent postgrest from starting if DB is not available Oct 4, 2016
@begriffs begriffs added the enhancement a feature, ready for implementation label Oct 4, 2016
@begriffs
Copy link
Member

On the master branch it now checks for a db connection and dies on startup if it cannot connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a feature, ready for implementation
Development

No branches or pull requests

4 participants