Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwQCU-0005hQ-79 for pgsql-general@arkaria.postgresql.org; Mon, 04 Jan 2021 13:47:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kwQCS-0000mj-SA for pgsql-general@arkaria.postgresql.org; Mon, 04 Jan 2021 13:47:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwQCS-0000mJ-JS for pgsql-general@lists.postgresql.org; Mon, 04 Jan 2021 13:47:08 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwQCI-0004Pw-SB for pgsql-general@lists.postgresql.org; Mon, 04 Jan 2021 13:47:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Y9S+CIMIXD2Xr7nmnbURr+i4kFaZ0jq/abejdUtn7LE=; b=JEyNVOlp5zcrIyVDQgIFeefgS1 xMtKfTkq0gzWkpTxy3XZsBvpLHQBF9NpCdCJpLD+r0dFmILO1XT3Y6G8q28KA4xnT6Iu0Ou9Pg7VF 6UJtmvEZ/sz451S1c3c8MskYD3I70v0ZtcuvrXoQ/0B6KS7/XjaPo2z/yrEWOHsZUAqM=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1kwQCH-0004Lt-Ea; Mon, 04 Jan 2021 14:46:57 +0100 Date: Mon, 4 Jan 2021 14:46:57 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Hassan Akefirad Cc: pgsql-general@lists.postgresql.org Subject: Re: How to generate random string for all rows in postgres Message-ID: <20210104134657.GB561@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Jan 04, 2021 at 02:14:11PM +0100, Hassan Akefirad wrote: > I have foo table and would like to set bar column to a random string. I've got the following query: > update foo > set bar = array_to_string( > array(select string_agg(substring('0123456789bcdfghjkmnpqrstvwxyz', round(random() * 30)::integer, 1), '') > from generate_series(1, 9)), ''); > But it generates the random string once and reuse it for all rows. I asked people on SO and one of the giants answered ([1]here): Hi, first of all - there is no need to use array_to_string(array( ... )) just bar = (select string_agg). it will not work, for the reasons you said, but it's better not to overcomplicate stuff. For your case, I think I'd simply make a function for generating random strings: CREATE OR REPLACE FUNCTION random_string( int ) RETURNS TEXT as $$ SELECT string_agg(substring('0123456789bcdfghjkmnpqrstvwxyz', round(random() * 30)::integer, 1), '') FROM generate_series(1, $1); $$ language sql; And then use it like this: update foo set bar = random_string(9) I know it's not perfect, but: 1. it works 2. your query becomes easier to read/understand 3. as a side benefit you will get function for other use cases :) Best regards, depesz