> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superjoin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# New User in PostgreSQL

> Let's create a new user for PostgreSQL with read-only permission

To create a user in PostgreSQL and give them read-only permissions to a specific database, you can follow these steps.

<Info>This process involves using SQL commands within your PostgreSQL environment</Info>

<Steps titleSize="h3">
  <Step title="Log into your PostgreSQL server">
    You can do this by using a terminal or an SQL client, connecting with a user that has sufficient privileges to create other users and manage permissions, such as the `postgres` superuser.

    ```sql sql theme={null}
    psql -U postgres
    ```
  </Step>

  <Step title="Create the user">
    Use the `CREATE ROLE` or `CREATE USER` command to create a new user. `CREATE USER` is effectively the same as `CREATE ROLE` but with the LOGIN ability enabled by default.

    ```sql sql theme={null}
    CREATE USER readonly_user WITH PASSWORD 'secure_password';
    ```

    Replace readonly\_user with your desired username and secure\_password with a strong password.
  </Step>

  <Step title="Grant connect permissions to the database">
    You need to grant the new user permission to connect to the specific database.

    ```sql sql theme={null}
    GRANT CONNECT ON DATABASE target_database TO readonly_user;
    ```

    Replace target\_database with the name of the database to which you want to grant access.
  </Step>

  <Step title="Grant usage permissions on schemas">
    Typically, you'd want the user to read data from specific schemas (commonly the public schema).

    ```sql sql theme={null}
    GRANT USAGE ON SCHEMA public TO readonly_user;
    ```
  </Step>

  <Step title="Grant select permissions on tables and views">
    Finally, grant the SELECT permission on all tables and views within the schema. To automatically apply this to new tables as well, you can set up default privileges.

    ```sql sql theme={null}
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;
    ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_user;
    ```

    This gives the `readonly_user` the ability to read data from all existing and future tables in the `public` schema of `target_database`.
  </Step>

  <Step title="Exit PostgreSQL session">
    After you're done, you can exit the PostgreSQL command line by typing.

    ```sql sql theme={null}
    \q
    ```
  </Step>
</Steps>
