Client#

To run any queries, the first step is to construct a client.

The library offers both synchronous and asynchronous clients.

class postgrest.AsyncPostgrestClient#

PostgREST client.

__init__(base_url: str, *, schema: str = 'public', headers: Dict[str, str] = {'Accept': 'application/json', 'Content-Type': 'application/json'}, timeout: int | float | Timeout = 5) None#
async aclose() None#

Close the underlying HTTP connections.

from_(table: str) AsyncRequestBuilder[Dict[str, Any]]#

Perform a table operation.

Parameters:

table – The name of the table

Returns:

AsyncRequestBuilder

table(table: str) AsyncRequestBuilder[Dict[str, Any]]#

Alias to from_().

from_table(table: str) AsyncRequestBuilder#

Alias to from_().

Deprecated since version 0.2.0: This will be removed in 1.0.0. Use self.from_() instead

rpc(func: str, params: dict) AsyncRPCFilterRequestBuilder[Any]#

Perform a stored procedure call.

Parameters:
  • func – The name of the remote procedure to run.

  • params – The parameters to be passed to the remote procedure.

Returns:

AsyncRPCFilterRequestBuilder

Example

await client.rpc("foobar", {"arg": "value"}).execute()

Changed in version 0.10.9: This method now returns a AsyncRPCFilterRequestBuilder.

Changed in version 0.10.2: This method now returns a AsyncFilterRequestBuilder which allows you to filter on the RPC’s resultset.

auth(token: str | None, *, username: str | bytes | None = None, password: str | bytes = '')#

Authenticate the client with either bearer token or basic authentication.

Raises:

ValueError – If neither authentication scheme is provided.

Note

Bearer token is preferred if both ones are provided.

schema(schema: str)#

Switch to another schema.

class postgrest.SyncPostgrestClient#

PostgREST client.

__init__(base_url: str, *, schema: str = 'public', headers: Dict[str, str] = {'Accept': 'application/json', 'Content-Type': 'application/json'}, timeout: int | float | Timeout = 5) None#
aclose() None#

Close the underlying HTTP connections.

from_(table: str) SyncRequestBuilder[Dict[str, Any]]#

Perform a table operation.

Parameters:

table – The name of the table

Returns:

AsyncRequestBuilder

table(table: str) SyncRequestBuilder[Dict[str, Any]]#

Alias to from_().

from_table(table: str) SyncRequestBuilder#

Alias to from_().

Deprecated since version 0.2.0: This will be removed in 1.0.0. Use self.from_() instead

rpc(func: str, params: dict) SyncRPCFilterRequestBuilder[Any]#

Perform a stored procedure call.

Parameters:
  • func – The name of the remote procedure to run.

  • params – The parameters to be passed to the remote procedure.

Returns:

AsyncRPCFilterRequestBuilder

Example

await client.rpc("foobar", {"arg": "value"}).execute()

Changed in version 0.10.9: This method now returns a AsyncRPCFilterRequestBuilder.

Changed in version 0.10.2: This method now returns a AsyncFilterRequestBuilder which allows you to filter on the RPC’s resultset.

auth(token: str | None, *, username: str | bytes | None = None, password: str | bytes = '')#

Authenticate the client with either bearer token or basic authentication.

Raises:

ValueError – If neither authentication scheme is provided.

Note

Bearer token is preferred if both ones are provided.

schema(schema: str)#

Switch to another schema.