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

role "xyz" already exists #28

Open
kjcsb1 opened this issue Mar 17, 2023 · 3 comments
Open

role "xyz" already exists #28

kjcsb1 opened this issue Mar 17, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@kjcsb1
Copy link

kjcsb1 commented Mar 17, 2023

Steps to reproduce:

tusker clean
tusker --verbose diff
Creating databases...
Creating migrated schema...
Creating original schema...
- db/init/base_db_entities/00-initial-schema.sql
Error executing SQL file db/init/base_db_entities/00-initial-schema.sql: (psycopg2.errors.DuplicateObject) role "xyz" already exists

Version

tusker 0.5.1

00-initial-schema.sql

-- Set up realtime
create schema if not exists realtime;
-- create publication xyz_realtime; -- defaults to empty publication
create publication xyz_realtime;

-- super admin
create user xyz;

Note:

  1. I had run tusker diff previously and it failed which presumably created the role which tusker clean doesn't remove
  2. I have tried setting privileges = false and privileges = true in tusker.toml with the same results
@bikeshedder
Copy link
Owner

Users and roles are database global and not part of the schema.
Therefore they can't be part of a migration and cause all sorts of troubles.

There is virtually nothing we can do about this as this and the best solution I came up for is having a separate create-roles.sql file which creates all the needed roles (and users) for me.

Since there is no CREATE USER ... IF NOT EXISTS you can use the following workaround:

DO $do$
BEGIN

IF NOT EXISTS (
    SELECT FROM pg_catalog.pg_user WHERE usename = 'xyz')
THEN
    CREATE USER xyz;
END IF;

END
$do$;

If you do find a better way to manage users and roles please let me know.

@bikeshedder bikeshedder added the documentation Improvements or additions to documentation label Mar 22, 2023
@bikeshedder
Copy link
Owner

bikeshedder commented Mar 22, 2023

Just reopening this as this might be useful to add a note to the tusker documentation.

@bikeshedder bikeshedder reopened this Mar 22, 2023
@kjcsb1
Copy link
Author

kjcsb1 commented Mar 31, 2023

I used this technique and it works well thanks. For a role, the syntax I used is:

DO $do$ BEGIN IF NOT EXISTS (
  SELECT
  FROM pg_catalog.pg_roles
  WHERE rolname = 'abc'
) THEN CREATE role abc;
END IF;
END $do$;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants