Importing existing database into new Dokku application - postgresql

I have an existing application that I am trying to port over to Dokku, and part of that is getting the existing data store copied over. I am using the Dokku Postgres plugin, but am having trouble getting the database ported over.
In my existing app I am creating a dump of the database:
// Create dump file
pg_dump app_database > db.dump
// Copy over to server hosting Dokku app
scp db.dump sshdetails
// SSH into new server, then attempt to import dump file
dokku postgres:import db < db.dump
When I run the last command, I get the message:
pg_restore: error: input file appears to be a text format dump. Please use psql.
I have tried formatting the dump in a few different formats but no luck. Any advice would be greatly appreciated. Thanks

From the source of dokku postgres:export command in dokku-postgres plugin :
docker exec "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w "$DATABASE_NAME"
The dokku postgres:export command have -Fc argument for pg_dump which export dump as archive suitable for input into pg_restore (more information into pg_dump man page) and pg_restore command seems to only accept custom format from pg_dump (cf. pg_restore man), so you should use the same -Fc argument in pg_dump command.

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.

Restore Postgres database using pg_restore over SSH

I have a database server without much disk space, so I took a backup of the entire db (let's just call it redblue) and saved it locally using the following command (I don't have pg running on my computer):
ssh admin#w.x.y.z "pg_dump -U postgres redblue -h localhost " \
>> db_backup_redblue.sql
I'd like to now restore it to another server (1.2.3.4) which contains an older version of "redblue" database - however wanted to ask if this is right before I try it:
ssh admin#1.2.3.4 "pg_restore -U postgres -C redblue" \
<< db_backup_redblue.sql
I wasn't sure if I need to do -C with the name of the db or not?
Will the above command overwrite/restore the remote database with the file I have locally?
Thanks!
No, that will do nothing good.
You have to start pg_restore on the machine where the dump is. Actually, since this is a plain format dump, you have to use psql rather than pg_restore:
psql -h 1.2.3.4 -U postgres -d redblue -f db_backup_redblue.sql
That requires that there is already an empty database redblue on the target system.
If you want to replace an existing database, you have to use the --clean and --create options with pg_dump.
If you want to use SSL, you'll have to configure the PostgreSQL server to accept SSL connections, see the documentation.
I'd recommend the “custom” format of pg_dump.
Of course, you can do this :) Assuming you use ssh keys to authorize user from source host to destination host.
On the source host you do the pg_dump, then pipe through ssh to destination host like this:
pg_dump -C nextcloud | ssh -i .ssh/pg_nextcloud_key postgres#192.168.0.54 psql -d template1
Hope that helps ;)

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.

Importing a postgresql dump to Heroku

I have a java app and postgresql database to go with it that is running on Heroku. I can push my app just fine, but what about the DB contents? I have exported a full dump from the database, but I don't know how I could import that.
By googling, you can find about db:push which is a limited rubygem, not pushing all the stuff needed. I have sequences, bigint datatypes etc. I also tried importing using heroku pg:psql --app MYAPP < db_all.out which just connects and stops, and going to heroku pg:psql --app MYAPP and issuing \i db_all.out complaints about permissions.
How should I do it?
You can run the pg_restore command from your local machine using the credentials given by heroku pg:credentials HEROKU_POSTGRESQL_<COLOR>.
To help others who still stumble upon this issue, what works for me is hgmnz's answer, but with a few modifications.
To be more precise:
Create a dump from the source PostgreSQL database
$ PGPASSWORD=YOUR_PG_PASSWORD pg_dump -Fc --no-acl --no-owner -h localhost -U YOUR_PG_USER YOUR_DB_NAME > YOUR_DB_NAME.dump
Get the Heroku Postgres credentials for your heroku app
$ heroku pg:credentials:url -a YOUR_APP_NAME
Attempt to import PostgreSQL dump to Heroku using the credentials above
$ pg_restore --verbose --clean --no-acl --no-owner -h HOSTNAME -U USER -d DATABASE -p PORT PATH/TO/YOUR_DB_NAME.dump --password
Enter the password received from the Heroku Postgres credentials
It should then import the dump successfully
This is very simple and had worked for me:
heroku pg:psql -a {YOUR_APP} -f {YOUR_DUMP_PATH}
I find this a better way then using the standard input syntax (like in the OP example), since it uses an option given by the Heroku command itself.
You may want to check if your dump file it's OK before submitting (in my case it wasn't).