How to backup/restore under a different db name with pg_dump? - postgresql

Using pg_dump under an AWS EC2, I want to make a copy of an existing database but under a new database name, potentially on a different instance.
I tried something like this
pg_dump -C --no-owner --no-tablespaces --dbname=eric --host=fs-......eu-central-1.rds.amazonaws.com --port=5432 --username=sa --password | psql -h fs-...SameOrOtherName.eu-central-1.rds.amazonaws.com --port=5432 --username=sa --password --dbname=new_eric
This comes back with the error
"psql: FATAL: database "new_eric" does not exist"
I am trying to avoid generating a pg_dump file, then modifying it, then reloading it...
I suppose I could "sed" to replace some stuff on the fly...?

Related

Copying Postgresql DB dump from remote server to local

I want to take a DB dump from a remote server and then copy this dump to my local.
I tried couple of commands but didn't worked.
Lastly I tried the command below;
pg_dump -h 10.10.10.70 -p 5432 -U postgres -d mydb | gzip > db1.gz
I succesffully take the DB and tried with restore from Pgadmin, it gives;
pg_restore: error: input file appears to be a text format dump. Please use psql
But at this point I can't use psql, I have to use Pgadmin and not sure if I'm able to get successfully DB dump to my local. I mean I can't verify with restore.
How can I take DB dump from remote server to my local?
Thanks!
Use the "custom" format:
pg_dump -F c -h 10.10.10.70 -p 5432 -U postgres -f mydb.dmp mydb
That can be restores with pg_restore and hence with pgAdmin.
You do not have to use pgAdmin. pgAdmin uses pg_restore, and there is nothing that keeps you from using it too.

Postgres ERROR: permission denied for table

I have a dump file generated by using pg_dump from a remote database. I am using the file to restore and populate a database using psql.
I did not have the user and database name. I peeked into the sql file and figured out the user and database.
Here are my steps:
1. psql -h <> -u <> -d ( I used the superuser here).
2. created the user as the application needs to access using this user only.
3. changed the owner of the database to the user created in step 2 using Pgadmin.
I am unable to access any tables and getting the error -- ERROR: permission denied for table
Please Help !
Thanks
It is better if you use dump and restore instead of psql for restoring the database.
e.g
e.g suppose you have database dbname_source in remoteDB and dbname_target in localDB then you can use the below commands for dump and restore
./pg_dump -U postgres -p 5432 -h <remote_IP> -Ft dbname_source > dump_of_source.sql.tar
./pg_restore -U postgres -h localhost -p 5432 -d dbname_target dump_of_source.sql.tar

postgresql pg_dumpall password error

postgresql 9.4 following this cheat sheet http://www.postgresonline.com/special_feature.php?sf_name=postgresql90_pg_dumprestore_cheatsheet&outputformat=html trying to backup
here is my command
C:\Program Files\PostgreSQL\9.4\bin>pg_dumpall -h arcserver -U postgres -w -p 5433 -f "R:\Data\LUCZ_2017\PostgreSQL_Backup\lucz.sql"
then it gives me this strange error
pg_dumpall: could not connect to database "template1": fe_sendauth: no password supplied
what is template1? as you can see in the picture there is no template1?
I put in the -w so it doesnt ask me for the password..
what am I doing wrong here?
pg_dumpall will try to dump the entire cluster (all databases that the engine serves). If you need this functionality, you probably only want to dump your lucz database (I'm assuming). Try using the -d option to specify the database to dump.
For help:
pg_dumpall --help
or https://www.postgresql.org/docs/9.4/static/app-pg-dumpall.html

pg_dump fails with error: `FATAL: role "username" does not exist`

I have a db named backupdb, I want to import this to my local rails app so I want to take a dump of it.
When I am running pg_dump backupdb, I am getting below error.
pg_dump: [archiver (db)] connection to database "backupdb" failed: FATAL: role "username" does not exist
what's wrong here. Please help.
I downloaded the db from my email and then trying to create a dump so I can import it to my local rails app
Try this way, it works!!
$ pg_dump -h localhost -U postgres -Fc mydb > db.dump
This command will take the backup of complete database
pg_dump -h localhost -U "dbuser" "dbname" -Fc > "pathfilename.backup"
**ex:** pg_dump -h localhost -U mani manidb - Fc > "thamesdb.backup"
for more pg_dump formats please refer to this answer
You're giving "username" as username, which does not exist. You need to pass a username that exists (postgres would probably do).
add parameters --username=postgres --password and it will ask you for the password for user postgres. (you might have security set to trust in your pg_hba.conf in which case leaving out --password would work.
If someone has the same problem using intellij, here is what helped me:
In my case the Problem was, that i right clicked the datasource, but you have to open it and right click the database itself. So in the image below don't click on localDB. Right click the database name and then do a dump.
Hope this will help someone to solve this confusing UX problem. :D
I was having the same problem but after trying out different permutations of the command options while referencing the documentation, I was able to get it running with this order of options. The command I used is:
pg_dump.exe -h localhost -U postgres -t "shop*" -t "core*" -t "profiles*" -f "path/to/output/dir_or_file" mydbname
Where:
option prefix
option
params
-h
host
localhost
-U
username
postgres
-t
Tables
Any tables whose name starts with (shop, core, or profiles (NOTE: for django app tables in my case))
-f
file out
path where I want to save the dumped file
(End of cmd)
database
mydbname
Note: as I have multiple versions of postgresql on my computer, I had to call the newest pg_dump executable. (Just for reference)

Dumping Database to Heroku app

I've been trying to dump a database file to my heroku app.
I generated a PG dump file by calling
pg_dump mydb > db.sql
Then I uploaded that to an amazons3 and uploaded it to heroku via:
C:\Users\user\Documents\GitHub\gvf-api>heroku pgbackups:restore
HEROKU_POSTGRESQL_GREEN_URL 's3.amazonaws.com/mydb'
The fictional url really works so that's not the issue
I get a Retrieving... done and a Restoring... done message when I run the call.
Problem is no data in the tables.
also I've used the dump file to re-create the database on my local machine successfully.
Any suggestions?
I added the following modifiers to the dump command as suggested here,
pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
After a few minutes the data did show up.