Git Product home page Git Product logo

postgrest-heroku's Introduction

PostgREST on Heroku

The best way to build an API, now for Heroku. Updated for PostgREST >= v9.0.0

Free and hobby Heroku PostgreSQL add-on do not support having multiple database roles (e.g. read-only or read-write roles).

Heroku Postgres paid tiers do support multiple database roles, though you'll have to create them through Heroku Postgres Credentials as explained below.

To Deploy PostgREST on Heroku

  1. Log into Heroku using the Heroku CLI:

    # If you have multiple Heroku accounts, use flag '--interactive' to switch between them
    heroku login --interactive
  2. Create a new Heroku app using this Heroku buildpack:

    mkdir ${YOUR_APP_NAME}
    cd ${YOUR_APP_NAME}
    git init .
    
    heroku apps:create ${YOUR_APP_NAME} --buildpack https://github.com/PostgREST/postgrest-heroku.git
    heroku git:remote -a ${YOUR_APP_NAME}
  3. Create a new Heroku Postgres add-on attached to the app and keep notes of the assigned add-on name (e.g. postgresql-curly-58902) referred later as ${HEROKU_PG_DB_NAME}

    heroku addons:create heroku-postgresql:standard-0 -a ${YOUR_APP_NAME}
    # wait until the add-on is available
    heroku pg:wait -a ${YOUR_APP_NAME}
  4. Create the necessary user roles according to the PostgREST documentation:

    heroku pg:credentials:create --name api_user -a ${YOUR_APP_NAME}
    # use the following command to ensure the new credential state is active before attaching it
    heroku pg:credentials -a ${YOUR_APP_NAME}
    
    heroku addons:attach ${HEROKU_PG_DB_NAME} --credential api_user -a ${YOUR_APP_NAME}
  5. Connect to the Postgres database and create some sample data:

    heroku psql -a ${YOUR_APP_NAME}
    # from the psql command prompt execute the following commands:
    create schema api;
    
    create table api.todos (
    id serial primary key,
    done boolean not null default false,
    task text not null,
    due timestamptz
    );
    
    insert into api.todos (task) values
    ('finish tutorial 0'), ('pat self on back');
    
    grant usage on schema api to api_user;
    grant select on api.todos to api_user;
  6. Create the Procfile:

    web: PGRST_SERVER_HOST=0.0.0.0 PGRST_SERVER_PORT=${PORT} PGRST_DB_URI=${PGRST_DB_URI:-${DATABASE_URL}} ./postgrest-${POSTGREST_VER}

    Set the following environment variables on Heroku:

    heroku config:set POSTGREST_VER=10.0.0
    heroku config:set PGRST_DB_SCHEMA=api
    heroku config:set PGRST_DB_ANON_ROLE=api_user
    

    PGRST_DB_URI can be set if an external database is used or if it's different from the default Heroku DATABASE_URL. This latter is used if nothing is provided.
    POSTGREST_VER is mandatory to select and build the required PostgREST release.

    See https://postgrest.org/en/stable/configuration.html#environment-variables for the full list of environment variables.

  7. Build and deploy your app:

    git add Procfile
    git commit -m "PostgREST on Heroku"
    git push heroku master

    Your Heroku app should be live at ${YOUR_APP_NAME}.herokuapp.com

  8. Test your app

    From a terminal display the application logs:

    heroku logs -t

    From a different terminal retrieve with curl the records previously created:

    curl https://${YOUR_APP_NAME}.herokuapp.com/todos

    and test that any attempt to modify the table via a read-only user is not allowed:

    curl https://${YOUR_APP_NAME}.herokuapp.com/todos -X POST \
     -H "Content-Type: application/json" \
     -d '{"task": "do bad thing"}'

postgrest-heroku's People

Contributors

abernicchia-heroku avatar begriffs avatar christiaanwesterbeek avatar diogob avatar fohlen avatar groussac avatar jamiefolsom avatar kofigumbs avatar martingms avatar steve-chavez avatar wolfgangwalther avatar wub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

postgrest-heroku's Issues

Basic auth

From README: "Note that the Heroku PostgreSQL addon will not work because it does not support having multiple database roles."

Could this be worked around by having postgrest support basic auth? postgrest --basic-auth <user>:<pass>? I'll be happy if this is implemented, as I can just pass --anonymous ${AUTH_USER} and be done with it without having to worry about the whole auth system (I'm the only user).

How to specify the role claim key

Some keys may use " (e.g ."https://somedomain/key") and will break the deployment. With the JWT_SECRET we can specify a base64 encoded string, but this is not possible with the ROLE_CLAIM_KEY

postgrest on heroku -- issue with crossdomain requests

I am able to run a postgrest server locally. Both the js front end app and the rails app seems to run fine on my local machine, but I'm running into an error when I try to run it on heroku.

I can't seem to get the API to run properly while running the server on Heroku

This is the error I'm receiving:

XMLHttpRequest cannot load https://api.citizensupported.org/user_details?id=eq.2. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.citizensupported.org' is therefore not allowed access. The response had HTTP status code 503.

I'm using the button at https://elements.heroku.com/buttons/begriffs/postgrest to deploy the postgrest server -- I think this recently got upgraded to v0.4.0.0

Any advice?

I'm not sure if I'm following the right approach to running a postgrest server on heroku.

Web process failed to bind to $PORT within 60 seconds of launch

I created a fresh Heroku app and Amazon RDS instance for Postgres. I can access de Postgres database with some gui (Postico).

For the Heroku app, I followed the procedure described here: https://github.com/begriffs/postgrest-heroku. So:

# first set up a new app
heroku apps:create --buildpack https://github.com/begriffs/postgrest-heroku.git

# now fill in the values specific to your database
heroku config:set POSTGREST_VER=0.4.0.0
heroku config:set DB_URI=postgres://myusername:[email protected]/mydbname
heroku config:set DB_SCHEMA=testschema
heroku config:set DB_ANON_ROLE=testrole
heroku config:set DB_POOL=10

git push heroku master

Now when I fire up the app, by going to the app url, I get an application error and this is in the log file:

2017-05-24T19:08:49.197400+00:00 heroku[web.1]: Starting process with command `./env-to-config ./postgrest postgrest.conf`
2017-05-24T19:08:50.229037+00:00 app[web.1]: Listening on port 33496
2017-05-24T19:09:22.400036+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=myappname.herokuapp.com request_id=3382e614-0553-4275-936a-da635589facd fwd="83.162.179.9" dyno= connect= service= status=503 bytes= protocol=https
2017-05-24T19:09:49.667434+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-05-24T19:09:49.667434+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-05-24T19:09:49.784127+00:00 heroku[web.1]: Process exited with status 137
2017-05-24T19:09:49.792706+00:00 heroku[web.1]: State changed from starting to crashed

With the bind to $PORT being the cause of the crashing app.
Here you can see my config variables ion Heroku:
image

I don't know how to debug this as I am new to hosting any kind of other app on Heroku besides Node.js apps. I would sure appreciate some help here.

Publish the new buildpack to the Heroku Registry

   still pending, reposting it as an issue to keep track of it.

@steve-chavez yes, sure I can help! However, it is needed to find the original creator who registered the Heroku buildpack for the first time. As per the documentation https://devcenter.heroku.com/articles/buildpack-registry#namespaces-and-buildpack-ownership a buildpack can be owned by an individual (e.g. Heroku personal account) or a Heroku Team. If the latter option has been used you can add me to that Team so I can manage it, otherwise, I would need the user credentials to access.

Originally posted by @abernicchia-heroku in #41 (comment)

Heroku image hangs on request

Hi, sorry to bother, I've been trying to get this image to work for most of today and at my wits end.

So far I have:

  • set up an amazon postgres RDS and can log in using psql
  • created a new role and two user accounts
  • created the "1" schema and some test views
  • created a heroku image based on this buildpack
  • updated the config vars to match my database

When I launch any request to the postgrest server it just hangs and then times out. I have noticed that it crashes on startup if the db configs are incorrect, so I'm pretty sure something is working. The logs are not helpful and I don't know any haskell, so I thought I'd reach out for a clue?

Function in public schema cannot be found

I'm using PostgREST-Heroku to make API calls to a database on Heroku which uses Heroku-Connect to sync data to a Salesforce database (not sure if this is relevant).

The problem is that the schema against which I consume the API (named "salesforce") is using a function stored in the public schema (function is named "get_xmlbinary") and I'm receiving this error when trying to POST or PATCH rows
{
"hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
"details": null,
"code": "42883",
"message": "function get_xmlbinary() does not exist"
}

Calling the function with POST /rpc/get_xmlbinary produces a slightly different message:
"message": "function salesforce.get_xmlbinary() does not exist"

This is my postgrest.conf:
db-uri = "postgres://authenticator:<OMITTED>"
db-schema = "salesforce"
db-anon-role = "anonymous"
jwt-secret = "<OMITTED>"
db-extra-search-path = "public"

Setting the db-schema property to db-schema = "public" works fine and returns the expected result (a string), but this way I lose the possibility to access the salesforce schema.

This troubleshooting from Heroku didn't helped, as my search_path is as expected: https://help.heroku.com/OZ4RPNQS/how-do-i-resolve-function-get_xmlbinary-does-not-exist-errors-in-heroku-connect.

Any suggestion how to solve this issue? Thanks!

Add JWT_AUD value

Currently the JWT_AUD is not set during deployment. This is necessary when deploying from Auth0

Build fails on Heroku

I followed the directions and got the following error:

-----> PostgREST app detected
-----> Exporting config vars
       DB_ANON_ROLE
       DB_SCHEMA
       DB_URI
       POSTGREST_VER
-----> Downloading and caching libraries for PostgREST
#=#=#                                 -----> Could not find prebuilt postgrest
 !     Push rejected, failed to compile PostgREST app.
 !     Push failed

The config file isn't being parsed correctly.

I can run the postgrest locally, when trying to deploy to heroku I get these issues.

2021-06-26T01:44:10.781198+00:00 heroku[web.1]: Starting process with command `./env-to-config ./postgrest-7.0.1 postgrest.conf`
2021-06-26T01:44:13.426518+00:00 heroku[web.1]: Process exited with status 1
2021-06-26T01:44:13.525478+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-26T01:44:13.349168+00:00 app[web.1]: Error parsing config file:
2021-06-26T01:44:13.349772+00:00 app[web.1]: postgrest.conf:3:24:
2021-06-26T01:44:13.349863+00:00 app[web.1]: |
2021-06-26T01:44:13.353094+00:00 app[web.1]: 3 | db-anon-role="wflvemda"db-uri="postgres://wflvemda:[email protected]/wflvemda"
2021-06-26T01:44:13.353863+00:00 app[web.1]: |                        ^
2021-06-26T01:44:13.354265+00:00 app[web.1]: unexpected 'd'
rom crashed to starting
2021-06-26T01:47:45.026374+00:00 heroku[web.1]: Starting process with command `./env-to-config ./postgrest-0.5.0.0 postgrest.conf`
2021-06-26T01:47:48.381780+00:00 heroku[web.1]: Process exited with status 1
2021-06-26T01:47:48.476001+00:00 heroku[web.1]: State changed from starting to crashed
2021-06-26T01:47:48.320573+00:00 app[web.1]: postgrest-0.5.0.0: ParseError "postgrest.conf" "endOfInput"

Missing buildpack support for latest postgres release v7.0.1

Hi,
seems that the buildpack's compile script does not support latest version v7.0.1 of postgrest precompiled binary since previous build relases used __ubuntu.tar.xz file suffix,
whereas the latest relelase states the following : "From this version onwards, the release page will include a single Linux static executable that can be run on any Linux distribution" (___linux-x64-static.tar.xz)

Setting postgrest version 7.0.1 on heroku yelds therefore to the following error:

-----> Could not find prebuilt postgrest-7.0.1

Missing LICENSE

@steve-chavez this package does not include a LICENSE file and this is required for proper contribution and use. I can create a PR and copy here the license file from the postgrest repo if you like. thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.