Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:19:04+00:00 2026-06-12T23:19:04+00:00

I am trying to setup a script to take a copy of a database

  • 0

I am trying to setup a script to take a copy of a database from one server to another.

Thanks to this post Copying PostgreSQL database to another server I have found a way to do that.

But what I need to do is change the name of the database during the copy.
I have thought about using sed and doing a simple text replace. But I am worried that this could corrupt the database.

Does any one know the proper way of doing this?

As requested here are the commands I am using

pg_dump -C -U remoteuser -h remoteServer dbname | psql -h localhost -U localadmin template1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:19:06+00:00Added an answer on June 12, 2026 at 11:19 pm

    Just restore to a different database. For pg_restore of -Fc dumps from pg_dump‘s custom format:

    createdb newdbname
    pg_restore --dbname newdbname database.dump
    

    For SQL-format dumps not created with the -C option to pg_dump:

    createdb newdbname
    psql -f database_dump.sql newdbname
    

    If you’re streaming the dump from a remote host, just omit -f database_dump.sql as the dump data is coming from stdin.

    You can’t easily CREATE DATABASE in the same command as your restore, because you need to connect to a different DB like template1 in order to create the new DB. So in your example you might:

    psql -h localhost -U localadmin template1 -c 'CREATE DATABASE newdb;'
    pg_dump -U remoteuser -h remoteServer dbname | psql -h localhost -U localadmin newdb
    

    Note the omission of the -C flag to pg_dump.

    The first command is just the longhand way of writing createdb -h localhost -U localadmin newdb.


    Update: If you’re stuck with a pg_dump created with the -C flag you can indeed just sed the dump so long as you’re extremely careful. There should only be four lines (one a comment) at the start of the file that refer to the database name. For the database name “regress” dumped with Pg 9.1’s pg_dump -C:

    --
    -- Name: regress; Type: DATABASE; Schema: -; Owner: craig
    --
    
    CREATE DATABASE regress WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
    
    
    ALTER DATABASE regress OWNER TO craig;
    
    \connect regress
    

    This can be transformed quite safely with three (or four if you want to rewrite the comment) very specific sed commands. Do not just do a global find and replace on the database name, though.

    sed \
      -e 's/^CREATE DATABASE regress/CREATE DATABASE newdbname/' \
      -e 's/^ALTER DATABASE regress/ALTER DATABASE newdbname/' \
      -e 's/^\\connect regress/\\connect newdbname/' \
      -e 's/^--Name: regress/--Name: newdbname/'
    

    This should be a last resort; it’s much better to just dump without -C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup the application server part of C2DM push messaging using this
I'm trying to setup an Apache/PHP/Postgresql server locally on my machine. I'm using Windows
I'm trying to get a bash script setup so it'll move files up to
I'm trying to setup a binary socket connection in Flash to a server other
I'm trying to setup a scheduled job that with one step that would insert
I am trying to setup a script to email using an exchange account. I
I'm trying to take Jenkins from a CI machine, and I would like to
I have a WPF project and I'm trying to setup a NAnt build script
I'm trying to setup a server at www.domain.com that will allow me to create
I am trying to setup a cronjob to execute a script every minute. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.