1

I'm using PostgreSQL with pgadmin. I need to replace multiple characters in all fields of type string, and in all tables in my database.

1
  • Welcome on stack, please read the stackoverflow.com/faq, and try to create a question with additional informations instead of a statement. Commented Jun 2, 2011 at 6:57

2 Answers 2

1

Use the below with caution - adjust as necessary, and make sure you review each and every resultant query (yes - this generates a resultset of queries) before copying the results and executing - as this may include queries that would attempt to modify views, calculated fields, system tables etc. etc.

I'm a T-SQL'er - but I believe the below should be valid in PostgresSQL.

SELECT 'UPDATE ' || TABLE_SCHEMA || '.' || TABLE_NAME ||
       ' SET ' || COLUMN_NAME || ' = REPLACE(' || 
       COLUMN_NAME || ', ''FROM'', ''TO'')'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE LIKE '%char%'

For additional information, which may provide ideas on how to adjust this query, check out INFORMATION_SCHEMA.COLUMNS.

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to exclude the information schema tables themselves
@Bohemian - +1. Indeed - and various other bits 'n' bobs - I'm not a hardcore Postgreser (?!) - the query is intended to be a starting point, there'll be a lot to gain from reviewing the output rather than blindly executing!
1

This will list all columns that are "character" type in your database:

SELECT t.tablename, a.attname AS column_name
FROM pg_attribute a
JOIN pg_class c ON a.attrelid = c.oid
JOIN pg_tables t ON t.tablename = c.relname
JOIN pg_type on pg_type.oid = a.atttypid
WHERE t.schemaname not in ('information_schema', 'pg_catalog')
and pg_type.typname in ('varchar', 'text', 'char')
order by 1,2;

Then you must go through and do the updates.

1 Comment

thank you very much. But as I do? I have no experience with sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.