Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq98J-0009eD-Pg for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 11:11:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tq98I-007aTF-41 for pgsql-general@arkaria.postgresql.org; Thu, 06 Mar 2025 11:11:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tq98H-007aT7-Ob for pgsql-general@lists.postgresql.org; Thu, 06 Mar 2025 11:11:17 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tq98D-001HlG-1O for pgsql-general@postgresql.org; Thu, 06 Mar 2025 11:11:16 +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; bh=33PTOToFgkayhb0Vlx9hxrbJI27liNTuJcmceWyof2o=; b=XNjCkYCTfYiWbf8dEO73j0IvLW uzSRp3QbZZm9T9eZ0kJATUyB6lYjkoe7/YO8J7ls57Yi36JUjT5xf8+D+KsKki5zmqOz/aa3dDx+t j+2Z7TgzaLvfB8Pvn3etTHfAGzvSCMkP0bzmerqXjovnGARHDVeBWYqtc8NRi/vpIU6U=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1tq98A-0007Hl-26; Thu, 06 Mar 2025 12:11:10 +0100 Date: Thu, 6 Mar 2025 12:11:10 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Ron Johnson Cc: pgsql-general Subject: Re: psql and regex not like Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Mar 06, 2025 at 04:37:56AM -0500, Ron Johnson wrote: > This statement runs great from the psql prompt. Does exactly what I want. > select datname from pg_database WHERE datname !~ 'template|postgres' ORDER > BY datname; > But it doesn't work so well from the bash prompt. Not escaping the "!" > generates a bunch of garbage, while escaping throws an sql syntax error. The problem is that ! is magical in bash. The solution is to not use it. Instead you can easily do: psql -Xc "select datname from pg_database WHERE not datname ~ 'template|postgres' ORDER BY datname;" Best regards, depesz