---
title: Charmhub | Deploy Charmed PostgreSQL K8s using Charmhub - The Open Operator
  Collection
description: Deploy the latest version of Charmed PostgreSQL K8s as a Kubernetes Operator
  on any cloud.
url: https://charmhub.io/postgresql-k8s/libraries/postgresql
---

# Charmed PostgreSQL K8s

[Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

* [Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")
* [Databases](https://charmhub.io/?filter=databases)

Platform:

14/stable 960

```
juju deploy postgresql-k8s --channel 14/stable --trust
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/postgresql-k8s/libraries/postgresql#drawer)

## charms.postgresql\_k8s.v0.postgresql

* [*Docstrings*Docstrings](https://charmhub.io/postgresql-k8s/libraries/postgresql)
  [*Code*Source code](https://charmhub.io/postgresql-k8s/libraries/postgresql/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.postgresql_k8s.v0.postgresql
    ```

    [Download postgresql.py](https://charmhub.io/postgresql-k8s/libraries/postgresql/download)
  + *Last updated* 21 Mar 2026
  + *Revision* Library version 0.58

PostgreSQL helper class.

The `postgresql` module provides methods for interacting with the PostgreSQL instance.

Any charm using this library should import the `psycopg2` or `psycopg2-binary` dependency.

---

Index

* [class PostgreSQLAssignGroupError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlassigngrouperror)
* [class PostgreSQLCreateDatabaseError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlcreatedatabaseerror)
* [class PostgreSQLCreateGroupError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlcreategrouperror)
* [class PostgreSQLCreateUserError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlcreateusererror)
* + [def \_\_init\_\_(
    self,
    message)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlcreateusererror-__init__)
* [class PostgreSQLDatabasesSetupError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqldatabasessetuperror)
* [class PostgreSQLDeleteUserError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqldeleteusererror)
* [class PostgreSQLEnableDisableExtensionError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlenabledisableextensionerror)
* [class PostgreSQLGetLastArchivedWALError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlgetlastarchivedwalerror)
* [class PostgreSQLGetCurrentTimelineError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlgetcurrenttimelineerror)
* [class PostgreSQLGetPostgreSQLVersionError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlgetpostgresqlversionerror)
* [class PostgreSQLListAccessibleDatabasesForUserError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqllistaccessibledatabasesforusererror)
* [class PostgreSQLListGroupsError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqllistgroupserror)
* [class PostgreSQLListUsersError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqllistuserserror)
* [class PostgreSQLUpdateUserPasswordError](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresqlupdateuserpassworderror)
* [class PostgreSQL](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql)
* + [def \_\_init\_\_(
    self,
    primary\_host,
    current\_host,
    user,
    password,
    database,
    system\_users)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-__init__)
* + [def create\_access\_groups(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-create_access_groups)
* + [def create\_database(
    self,
    database,
    user,
    plugins,
    client\_relations)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-create_database)
* + [def create\_user(
    self,
    user,
    password,
    admin,
    extra\_user\_roles)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-create_user)
* + [def delete\_user(
    self,
    user)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-delete_user)
* + [def grant\_internal\_access\_group\_memberships(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-grant_internal_access_group_memberships)
* + [def grant\_relation\_access\_group\_memberships(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-grant_relation_access_group_memberships)
* + [def enable\_disable\_extensions(
    self,
    extensions,
    database)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-enable_disable_extensions)
* + [def get\_last\_archived\_wal(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_last_archived_wal)
* + [def get\_current\_timeline(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_current_timeline)
* + [def get\_postgresql\_text\_search\_configs(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_postgresql_text_search_configs)
* + [def get\_postgresql\_timezones(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_postgresql_timezones)
* + [def get\_postgresql\_default\_table\_access\_methods(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_postgresql_default_table_access_methods)
* + [def get\_postgresql\_version(
    self,
    current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-get_postgresql_version)
* + [def is\_tls\_enabled(
    self,
    check\_current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-is_tls_enabled)
* + [def list\_access\_groups(
    self,
    current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-list_access_groups)
* + [def list\_accessible\_databases\_for\_user(
    self,
    user,
    current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-list_accessible_databases_for_user)
* + [def list\_users(
    self,
    group,
    current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-list_users)
* + [def list\_users\_from\_relation(
    self,
    current\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-list_users_from_relation)
* + [def list\_valid\_privileges\_and\_roles(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-list_valid_privileges_and_roles)
* + [def set\_up\_database(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-set_up_database)
* + [def update\_user\_password(
    self,
    username,
    password,
    database\_host)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-update_user_password)
* + [def is\_restart\_pending(
    self)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-is_restart_pending)
* + [def build\_postgresql\_group\_map(
    group\_map)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-build_postgresql_group_map)
* + [def build\_postgresql\_parameters(
    config\_options,
    available\_memory,
    limit\_memory)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-build_postgresql_parameters)
* + [def validate\_date\_style(
    self,
    date\_style)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-validate_date_style)
* + [def validate\_group\_map(
    self,
    group\_map)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-validate_group_map)
* + [def is\_user\_in\_hba(
    self,
    username)](https://charmhub.io/postgresql-k8s/libraries/postgresql#postgresql-is_user_in_hba)

#### class PostgreSQLAssignGroupError

Description

Exception raised when assigning to a group fails. None

#### class PostgreSQLCreateDatabaseError

Description

Exception raised when creating a database fails. None

#### class PostgreSQLCreateGroupError

Description

Exception raised when creating a group fails. None

#### class PostgreSQLCreateUserError

Description

Exception raised when creating a user fails. None

Methods

PostgreSQLCreateUserError.
\_\_init\_\_(

*self*

,
message
)

#### class PostgreSQLDatabasesSetupError

Description

Exception raised when the databases setup fails. None

#### class PostgreSQLDeleteUserError

Description

Exception raised when deleting a user fails. None

#### class PostgreSQLEnableDisableExtensionError

Description

Exception raised when enabling/disabling an extension fails. None

#### class PostgreSQLGetLastArchivedWALError

Description

Exception raised when retrieving last archived WAL fails. None

#### class PostgreSQLGetCurrentTimelineError

Description

Exception raised when retrieving current timeline id for the PostgreSQL unit fails. None

#### class PostgreSQLGetPostgreSQLVersionError

Description

Exception raised when retrieving PostgreSQL version fails. None

#### class PostgreSQLListAccessibleDatabasesForUserError

Description

Exception raised when retrieving the accessible databases for a user fails. None

#### class PostgreSQLListGroupsError

Description

Exception raised when retrieving PostgreSQL groups list fails. None

#### class PostgreSQLListUsersError

Description

Exception raised when retrieving PostgreSQL users list fails. None

#### class PostgreSQLUpdateUserPasswordError

Description

Exception raised when updating a user password fails. None

#### class PostgreSQL

Description

Class to encapsulate all operations related to interacting with PostgreSQL instance. None

Methods

PostgreSQL.
\_\_init\_\_(

*self*

,
primary\_host: str

,
current\_host: str

,
user: str

,
password: str

,
database: str

,
system\_users
)

PostgreSQL.
create\_access\_groups(

*self*
)

Description

Create access groups to distinguish HBA authentication methods. None

PostgreSQL.
create\_database(

*self*

,
database: str

,
user: str

,
plugins

,
client\_relations
)

Creates a new database and grant privileges to a user on it.

Arguments

database

database to be created.

user

user that will have access to the database.

plugins

extensions to enable in the new database.

client\_relations

current established client relations.

PostgreSQL.
create\_user(

*self*

,
user: str

,
password

,
admin: bool

,
extra\_user\_roles
)

Creates a database user.

Arguments

user

user to be created.

password

password to be assigned to the user.

admin

whether the user should have additional admin privileges.

extra\_user\_roles

additional privileges and/or roles to be assigned to the user.

PostgreSQL.
delete\_user(

*self*

,
user: str
)

Deletes a database user.

Arguments

user

user to be deleted.

PostgreSQL.
grant\_internal\_access\_group\_memberships(

*self*
)

Description

Grant membership to the internal access-group to existing internal users. None

PostgreSQL.
grant\_relation\_access\_group\_memberships(

*self*
)

Description

Grant membership to the relation access-group to existing relation users. None

PostgreSQL.
enable\_disable\_extensions(

*self*

,
extensions

,
database
)

Enables or disables a PostgreSQL extension.

Description

Args:
extensions: the name of the extensions.
database: optional database where to enable/disable the extension.

Raises:
PostgreSQLEnableDisableExtensionError if the operation fails.

PostgreSQL.
get\_last\_archived\_wal(

*self*
)

Description

Get the name of the last archived wal for the current PostgreSQL cluster. None

PostgreSQL.
get\_current\_timeline(

*self*
)

Description

Get the timeline id for the current PostgreSQL unit. None

PostgreSQL.
get\_postgresql\_text\_search\_configs(

*self*
)

Returns the PostgreSQL available text search configs.

Returns

Set of PostgreSQL text search configs.

PostgreSQL.
get\_postgresql\_timezones(

*self*
)

Returns the PostgreSQL available timezones.

Returns

Set of PostgreSQL timezones.

PostgreSQL.
get\_postgresql\_default\_table\_access\_methods(

*self*
)

Returns the PostgreSQL available table access methods.

Returns

Set of PostgreSQL table access methods.

PostgreSQL.
get\_postgresql\_version(

*self*

,
current\_host
)

Returns the PostgreSQL version.

Returns

PostgreSQL version number.

PostgreSQL.
is\_tls\_enabled(

*self*

,
check\_current\_host: bool
)

Returns whether TLS is enabled.

Arguments

check\_current\_host

whether to check the current host
instead of the primary host.

Returns

whether TLS is enabled.

PostgreSQL.
list\_access\_groups(

*self*

,
current\_host
)

Returns the list of PostgreSQL database access groups.

Arguments

current\_host

whether to check the current host
instead of the primary host.

Returns

List of PostgreSQL database access groups.

PostgreSQL.
list\_accessible\_databases\_for\_user(

*self*

,
user: str

,
current\_host
)

Returns the list of accessible databases for a specific user.

Arguments

user

the user to check.

current\_host

whether to check the current host
instead of the primary host.

Returns

List of accessible database (the ones where
the user has the CONNECT privilege).

PostgreSQL.
list\_users(

*self*

,
group

,
current\_host
)

Returns the list of PostgreSQL database users.

Arguments

group

optional group to filter the users.

current\_host

whether to check the current host
instead of the primary host.

Returns

List of PostgreSQL database users.

PostgreSQL.
list\_users\_from\_relation(

*self*

,
current\_host
)

Returns the list of PostgreSQL database users that were created by a relation.

Arguments

current\_host

whether to check the current host
instead of the primary host.

Returns

List of PostgreSQL database users.

PostgreSQL.
list\_valid\_privileges\_and\_roles(

*self*
)

Returns two sets with valid privileges and roles.

Returns

Tuple containing two sets: the first with valid privileges
and the second with valid roles.

PostgreSQL.
set\_up\_database(

*self*
)

Description

Set up postgres database with the right permissions. None

PostgreSQL.
update\_user\_password(

*self*

,
username: str

,
password: str

,
database\_host
)

Update a user password.

Description

Args:
username: the user to update the password.
password: the new password for the user.
database\_host: the host to connect to.

Raises:
PostgreSQLUpdateUserPasswordError if the password couldn't be changed.

PostgreSQL.
is\_restart\_pending(

*self*
)

Description

Query pg\_settings for pending restart. None

PostgreSQL.
build\_postgresql\_group\_map(

group\_map
)

Build the PostgreSQL authorization group-map.

Arguments

group\_map

serialized group-map with the following format:
<ldap\_group\_1>=<psql\_group\_1>,
<ldap\_group\_2>=<psql\_group\_2>,
...

Returns

List of LDAP group to PostgreSQL group tuples.

PostgreSQL.
build\_postgresql\_parameters(

config\_options: dict

,
available\_memory: int

,
limit\_memory
)

Builds the PostgreSQL parameters.

Arguments

config\_options

charm config options containing profile and PostgreSQL parameters.

available\_memory

available memory to use in calculation in bytes.

limit\_memory

(optional) limit memory to use in calculation in bytes.

Returns

Dictionary with the PostgreSQL parameters.

PostgreSQL.
validate\_date\_style(

*self*

,
date\_style: str
)

Validate a date style against PostgreSQL.

Returns

Whether the date style is valid.

PostgreSQL.
validate\_group\_map(

*self*

,
group\_map
)

Validate the PostgreSQL authorization group-map.

Arguments

group\_map

serialized group-map with the following format:
<ldap\_group\_1>=<psql\_group\_1>,
<ldap\_group\_2>=<psql\_group\_2>,
...

Returns

Whether the group-map is valid.

PostgreSQL.
is\_user\_in\_hba(

*self*

,
username: str
)

Description

Check if user was added in pg\_hba. None
