Transfer data between databases with PostgreSQL

前端 未结 9 1509
我寻月下人不归
我寻月下人不归 2020-12-22 17:51

I need to transfer some data from another database. The old database is called paw1.moviesDB and the new database is paw1. The schema of each table are the following.

<
9条回答
  •  佛祖请我去吃肉
    2020-12-22 18:18

    I just had to do this exact thing so I figured I'd post the recipe here. This assumes that both databases are on the same server.

    First, copy the table from the old db to the new db (because apparently you can't move data between databases). At the commandline:

    pg_dump -U postgres -t   | psql -U postgres -d 
    
    # Just adding extra space here so scrollbar doesn't hide the command
    

    Next, grant permissions of the copied table to the user of the new database. Log into psql:

    psql -U postgres -d 
    
    ALTER TABLE  OWNER TO ;
    
    \q
    

    Finally, copy data from the old table to the new table. Log in as the new user and then:

    INSERT INTO  (field1, field2, field3) 
    SELECT field1, field2, field3 from ;
    

    Done!

提交回复
热议问题