pg_dump postgres database from remote server when port 5432 is blocked

心已入冬 提交于 2019-12-03 01:28:35

问题


I'm trying to pg_dump a SQL database on a remote server in our DMZ. There are 2 problems.

1) there is n't a lot of space left on the remote server so the normal command run to locally backup the database pg_dump -C database > sqldatabase.sql.bak won't work due to space issues.

2) I also can't run the other version of pg_dump command to dump database from remote server to local server using:

pg_dump -C -h remotehost -U remoteuser db_name | psql localhost -U localuser db_name

as the server is in our DMZ and port 5432 is blocked. What I'm looking to see is if it is possible to pg_dump the database and immediatly save it (ssh or some other form) as a file to a remote server. What I was trying was: pg_dump -C testdb | ssh admin@ourserver.com | > /home/admin/testdb.sql.bak

Does anyone know if what i am trying to achieve is possible?


回答1:


You can connect with ssh to your remote server, do with the connect the pg_dump call and send the output back to stdout of local machine.

ssh user@remote_machine "pg_dump -U dbuser -h localhost -C --column-inserts" \
 >> backup_file_on_your_local_machine.sql

Edit: fixed a typo.




回答2:


let's create a backup from remote postgresql database using pg_dump:

pg_dump -h [host address] -Fc -o -U [database user] <database name> > [dump file]

later it could be restored at the same remote server using:

sudo -u postgres pg_restore -C mydb_backup.dump

Ex:

pg_dump -h 67.8.78.10 -Fc -o -U myuser mydb > mydb_backup.dump

complete (all databases and objects)

pg_dumpall -U myuser -h 67.8.78.10 --clean --file=mydb_backup.dump

restore from pg_dumpall --clean:

psql -f mydb_backup.dump postgres #it doesn't matter which db you select here

Copied from: https://codepad.co/snippet/73eKCuLx




回答3:


You can try to dump part of the table to a file in your local machine like this (assume your local machine has psql installed):

psql -h ${db_host} -p 5432 -U ${db_user} -d ${db_name} \
-c "\copy (SELECT * FROM my_table LIMIT 10000) to 'some_local_file.csv' csv;"

And you can import the exported csv into another db later like this:

COPY my_table FROM '/path/to/some_local_file.csv' WITH (FORMAT csv);



回答4:


One possible solution - pipe through ssh - has been mentioned.

You also could make your DB server listen on the public inet address, add a hostssl entry for your backup machine to pg_hba.conf, maybe configure a client certificate for security, and then simply run the dump on the client/backup machine with pg_dump -h dbserver.example.com ...

This is simpler for unattended backups.

For the configuration of the connection (sslmode) see also the supported environment variables.



来源:https://stackoverflow.com/questions/29648309/pg-dump-postgres-database-from-remote-server-when-port-5432-is-blocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!