Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I Want a function to create Table in the different database , Database name is the argument for the function and table structure and names are static {No need to pass as the arguments}

share|improve this question
    
even from "postgres" database ?? –  sri Jan 8 at 10:24
    
@a_horse_with_no_name It is possible using dblink, isn't it? –  dezso Jan 8 at 11:37
    
Are you aware of schemas? Most people who're trying to do this sort of thing are attempting it to do multi-tenant storage. If you ever think you might need to then query these tables between databases, stop what you're doing now and and use separate schemas in the same database instead. –  Craig Ringer Jan 8 at 12:53

1 Answer 1

Using the dblink extension, this is possible.

Just create a connection inside your function, supplying the database name:

SELECT dblink_connect('conn', format('hostaddr=127.0.0.1 port=5435 dbname=%I user=test password=test', 'foreign_test'));
SELECT dblink_exec('conn', 'CREATE TABLE t (id integer)');
SELECT dblink_disconnect('');

Note that using the format() function escapes your database name as necessary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.