{"id":3126,"date":"2025-04-26T08:22:01","date_gmt":"2025-04-26T08:22:01","guid":{"rendered":"https:\/\/w3buddy.com\/?p=3126"},"modified":"2026-01-15T12:44:25","modified_gmt":"2026-01-15T07:14:25","slug":"how-to-copy-data-from-prod-to-test-via-db-link","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/how-to-copy-data-from-prod-to-test-via-db-link\/","title":{"rendered":"How to Copy Data from Prod to Test via DB Link"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This guide helps you copy data from a table owned by another user in the <strong>production database<\/strong> into your own schema in the <strong>test database<\/strong> using a <strong>database link<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Create a Table in Your Schema in Production DB<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the data from the other user\u2019s table into a table under your own schema.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">-- Login to production DB as your user\nSQL>conn your_user\/your_password;\nSQL>show user;\n\nCREATE TABLE my_table AS\nSELECT * FROM source_user.source_table;\n\nselect count(*) from your_user.my_table;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\ud83d\udcdd <strong>Note<\/strong>: You need <strong>SELECT <\/strong>privilege on <strong>source_user.source_table<\/strong>. If not, ask your DBA to grant it:<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">GRANT SELECT ON source_user.source_table TO your_user;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Grant Access to Your Table in Prod (if needed)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This step allows your test DB to access your table via DB link.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">-- Optional: Grant select to PUBLIC or a specific user if needed\nGRANT SELECT ON my_table TO PUBLIC;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Create a DB Link in the Test Database<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a database link from <strong>test DB<\/strong> to your <strong>production DB user<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">-- Login to test DB\nCREATE DATABASE LINK prod_user_link\nCONNECT TO your_user IDENTIFIED BY your_password\nUSING 'PROD_TNS_ALIAS';<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\ud83d\udd01 Replace:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>your_user<\/strong> \u2192 your production DB username<\/li>\n\n\n\n<li><strong>your_password<\/strong> \u2192 your production DB password<\/li>\n\n\n\n<li><strong>&#8216;PROD_TNS_ALIAS&#8217;<\/strong> \u2192 TNS entry for production DB in <strong>tnsnames.ora<\/strong><\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Create the Table in Test DB (Only If It Doesn\u2019t Exist)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This creates the table in your test DB only if it\u2019s not already there.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">-- On test DB\nCREATE TABLE my_table AS\nSELECT * FROM your_user.my_table@prod_user_link WHERE 1=0;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>WHERE 1=0<\/strong> ensures the structure is copied without copying data.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Insert Data from Production to Test<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the actual data over the DB link.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-sql\">-- On test DB\nINSERT INTO my_table\nSELECT * FROM your_user.my_table@prod_user_link;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Done!<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You have now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Created a local copy of another user&#8217;s table in <strong>production<\/strong><\/li>\n\n\n\n<li>Created a <strong>DB link<\/strong> from <strong>test<\/strong> to <strong>prod<\/strong><\/li>\n\n\n\n<li>Inserted the data into your <strong>test DB<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let me know if you want this as a ready-to-run SQL script or with dynamic table names.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide helps you copy data from a table owned by another user in the production database into your own schema in the test database using a database link. Step 1: Create a Table in Your Schema in Production DB Copy the data from the other user\u2019s table into a table under your own schema. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225],"tags":[],"class_list":["post-3126","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=3126"}],"version-history":[{"count":1,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3126\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3126\/revisions\/3127"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=3126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}