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

doc: PL/pgSQL to convert between bytea and urlsafe base64 #7

Open
coolaj86 opened this issue Sep 19, 2024 · 0 comments
Open

doc: PL/pgSQL to convert between bytea and urlsafe base64 #7

coolaj86 opened this issue Sep 19, 2024 · 0 comments

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented Sep 19, 2024

PL/pgSQL function to encode bytea as urlsafe base64

CREATE OR REPLACE FUNCTION bytea_to_urlsafe_base64(bytes bytea)
RETURNS TEXT
LANGUAGE SQL
IMMUTABLE
STRICT
PARALLEL SAFE
AS $$
    SELECT replace(replace(rtrim(encode(bytes, 'base64'), '='), '+', '-'), '/', '_');
$$;

PL/pgSQL function to decode urlsafe base64 as bytea

CREATE OR REPLACE FUNCTION urlsafe_base64_to_bytea(urlbase64 TEXT)
RETURNS bytea
LANGUAGE SQL
IMMUTABLE
STRICT
PARALLEL SAFE
AS $$
    SELECT decode(
        REPLACE(REPLACE(urlbase64, '-', '+'), '_', '/') ||
        repeat('=', (4 - (length(urlbase64) % 4)) %4),
        'base64'
    );
$$;

License

The code snippets and documentation in this issue are licensed under the CC0-1.0 (Public Domain), as follows:

PostgreSQL URL-safe Base64 - convert between bytea and urlsafe base64

Authored in 2024 by AJ ONeal [email protected]
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see https://creativecommons.org/publicdomain/zero/1.0/

@coolaj86 coolaj86 changed the title add PL/pgSQL function to convert between bytea and urlsafe base64 add PL/pgSQL to convert between bytea and urlsafe base64 Sep 19, 2024
@coolaj86 coolaj86 changed the title add PL/pgSQL to convert between bytea and urlsafe base64 doc: PL/pgSQL to convert between bytea and urlsafe base64 Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant