For a postgres based database I need to mirror the table definitions and sequence numbers from one schema to another. For the purpose of copying the schema definitions, I've been able to use pg_dump with schema definition only, however documentation seems to indicate that sequence numbers are only exported when data export is selected.
Is there an easy to export the corresponding sequence numbers in the schema exportation or an easy way to transfer these values or is the only alternative to interface with the database from a scripting language?
Looking at the dump the pg_dump first writes the creation of the sequence and the corrects that start value with
SELECT pg_catalog.setval('tuutti_id_seq', 4, true);
So if you do a schema-only dump you can construct the statement from information schema, for example with SQL query:
SELECT 'SELECT pg_catalog.setval(''' || sequence_name || ''', ' || start_value || ', true);'
FROM information_schema.sequences;
Related
I need to copy the entire schema into the same database with tables, tables data, functions, sequences, views, material views. I can't use dump/restore method because I haven't got permission on the parent schema. I can't rename it and haven't got access to the console. I tried to use https://stackoverflow.com/a/59176810/9631920 but there are only tables and views.
UPDATE
https://stackoverflow.com/a/51715918/9631920 getting 2 errors when trying to use that function:
ERROR: column "max_value" does not exist Perhaps you meant to reference the column "dict_test_value_seq.last_value"
as I understand it happens because I can no longer get the max_value by selecting from the sequence in new versions of Postgres.
When I comment sequence block, I'm getting an error: query string argument of EXECUTE is null, line 177.
That means that we got null in EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ;
question is pretty simple, but can't seem to find a concrete answer anywhere.
I need to update all tables inside my postgresql schema to include a timestamp column with default NOW(). I'm wondering how I can do this via a query instead of having to go to each individual table. There are several hundred tables in the schema and they all just need to have the one column added with the default value.
Any help would be greatly appreciated!
The easy way with psql, run a query to generate the commands, save and run the results
-- Turn off headers:
\t
-- Use SQL to build SQL:
SELECT 'ALTER TABLE public.' || table_name || ' add fecha timestamp not null default now();'
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='public';
-- If the output looks good, write it to a file and run it:
\g out.tmp
\i out.tmp
-- or if you don't want the temporal file, use gexec to run it:
\gexec
I am new to DB2 Express-C and am wondering if a SQL statement can be run in DB2 that can go thru all tables and change (could be scramble, replace, random, whatever) any 10-dgit phone numbers found. In a nut shell, do a global search and replace on all fields, in all tables, to obfuscate any phone numbers (private info).
I need to hand off a client's DB2 database (17 tables) to a software vendor to do some reporting but I have been tasked with making sure NO real phone numbers get exposed in the process. I'm sure there is a SQL statement that could handle a single table (then I could go into DB2 Control Center and run it 17x) but I am looking for a quick, clean way to 'prep' this DB before sending it out.
Thanks in advance to you DB2 gurus!
Mike
If you want to hide information in db2, you should use these functions:
ENCRYPT
DECRYPT_BIN
DECRYPT_CHAR
GETHINT
Or the IBM Database Encryption Expert
The functions are for varchar, and I think you have a int column, so it does not work.
However, you can hide all telephone numbers from different tables by applying a UDF, and querying the catalog.
For example, your UDF (user defined function) could mutiply the telephone by a given number that you only know. Then, in order to decrypt it, you just have to divide by the same number.
Then, to apply this procedure to all tables, you have to query the catalog. For example,
select tabschema, tabname
from syscat.columns
where colname like 'PHONE%'
Once, you are sure of the tables, you could update them with your UDF
select 'update ' || trim(tabschema) || '.' || trim(tabname) || ' set ' || trim(colname) || ' = myUDF(' || colname || ', 5) ;' from syscat.columns where colname like 'PHONE%'
Let's say 5 is your encryption number.
You just have to execute the output in order to hide the telephones.
I want to export ONLY the sequences created in a Database created in PostgreSQL.
There is any option to do that?
Thank you!
You could write a query to generate a script that will create your existing sequence objects by querying this information schema view.
select *
from information_schema.sequences;
Something like this.
SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' || start_value || ';'
from information_schema.sequences;
I know its too old but today I had similar requirement so I tried to solve it the same way by creating a series of "CREATE SEQUENCE" queries which can be used to RE-create sequences on the other DB with bad import (missing sequences)
here is the SQL I used:
SELECT
'CREATE SEQUENCE '||c.relname||
' START '||(select setval(c.relname::text, nextval(c.relname::text)-1))
AS "CREATE SEQUENCE SQLs"
FROM
pg_class c
WHERE
c.relkind = 'S'
Maybe that can be helpful for someone.
Using DBeaver, you can
open a schema
select its sequences
crtl-F to search for the sequences you're interested in
crtl-A to select all of them
Right-click and select Generate SQL -> DDL
You will be given SQL statements to create all of the sequences selected.
I have a user in my DB2 database that I want to grant select rights on all tables and views for a given schema. Any thoughts on how to do that as one SQL statement?
In order to grant select to a given user, you have to "generate" the sentence for each table and view of a given schema. You can do it via the CLP with a query like this:
db2 -x "select 'grant select on table ' || rtrim(tabschema) || '.' || rtrim(tabname) || ' to user JOHN_DOE' from syscat.tables where tabschema like 'FOO%' and (type = 'T' or type = 'V')" | db2 +p -tv
This command line will generate the grants for user JOHN_DOE for all tables (T) and views (V) of any schema starting with FOO.
If you have many tables, the output will be very big and the internal buffer will be filled. Reissue the command by generating the grants for a smaller set of tables.
If you are not sure about what you are going to execute, issue the previous command without the final part (| db2 +p -tv), this will write the commands in the standard output. However, this part is the most important, because this executes the generated output.
For more details, please check the InfoCenter or my blog http://angocadb2.blogspot.com/2011/12/ejecutar-la-salida-de-un-query-en-clp.html (In Spanish)