Re: Update "usename" in pg_user

From: "Dischner, Anton" <Anton(dot)Dischner(at)med(dot)uni-muenchen(dot)de>
To: pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Update "usename" in pg_user
Date: 2023-11-14 14:38:57
Message-ID: 2143ad5560114b9c854858763b6326a3@med.uni-muenchen.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi all,

thats what Bard has to offer:

write me a bash script to rename all users for postgresql. i want to append "@email.com"

Sure, here is a bash script to rename all users for PostgreSQL and append "@email.com":
Bash#!/bin/bash

# Get all users
users=$(psql -c "SELECT usename FROM pg_user;" | grep -v "postgres")

# Rename all users
for user in $users; do
psql -c "ALTER USER $user RENAME TO $user(at)email(dot)com;"
done

# Display success message
echo "All users have been renamed successfully."

To use this script, simply save it as a file with a .sh extension (e.g., rename_users.sh) and make it executable by running the following command:
Bashchmod +x rename_users.sh

Then, you can run the script to rename all users by running the following command:
Bash./rename_users.sh

This script will rename all users in the current PostgreSQL database. If you want to rename all users in a specific database, you can specify the database name as an argument to the psql command. For example, to rename all users in the database "my_database", you would run the following command:
Bashpsql -d my_database -c "SELECT usename FROM pg_user;" | grep -v "postgres"

You can also modify the script to append a different string to the end of the usernames. For example, to append the string "@example.com", you would change the line "ALTER USER $user RENAME TO $user(at)email(dot)com;" to the following line:
BashALTER USER $user RENAME TO $user(at)example(dot)com;

Best,

Anton

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Holger Jakobs 2023-11-14 14:51:53 Re: Update "usename" in pg_user
Previous Message jacob ndinkwa 2023-11-14 13:28:34 How to configure Repmgr