1

What I need is basically to create read-only mirror of remote database on my local computer. It would be also neat if the mirror would be able to periodically update itself.

Version of the database is Postgresql 9.3.

The reason for doing this is that I need to perform selects to that database and on my internet connection it takes ages.

I have found this article but it requires access to that remote server which I currently do not have. So is there some way of doing it without having direct access to the server? What I have is only db name, domain, port, username and password.

7
  • if you don't have direct access to the db server, then you can't really be sure your copy will be accurate (or even possible to do). Commented Nov 25, 2014 at 20:50
  • Do you sometimes have any access to the database ? With a remote direct connection or via some other file-based transfert ? Commented Nov 25, 2014 at 20:51
  • Unfortunately, as I wrote, what I have is db name, domain, port, username and password. Nothing else :-( And what's more, when I tried the approach described in the article, I linked, I got FATAL: no pg_hba.conf entry for replication connection from host. However I really need only read-only access. So I thought there would be some other way around. Commented Nov 25, 2014 at 20:56
  • What is your operating system ? Do you have any scripting language available locally ? Commented Nov 25, 2014 at 21:01
  • Ubuntu 14.04. Locally I can do and install practically everything. Commented Nov 25, 2014 at 21:03

1 Answer 1

3

If you only have access to the database with username and password, I would create a simple script that drops the current local database and repopulate it from the freshly recovered database dump.

The script would look like this

#!/bin/bash
REMOTE_PSQL_OPTIONS="-h <remote_host> -p <remote_port> -U <remote_user>"
LOCAL_PSQL_OPTIONS="-h localhost -p <local_port> -U <local_user>"


# retrieve the database locally 
echo "Remote database fetch"
pg_dump $REMOTE_PSQL_OPTIONS <remote_db_name> > /tmp/my_db.sql

# install locally
echo "Local install in temporary database"
psql $LOCAL_PSQL_OPTIONS postgres "CREATE DATABASE my_tmp_db"
psql $LOCAL_PSQL_OPTIONS my_tmp_db -f /tmp/my_db.sql

# switch databases
echo "Installing the new database version"
psql $LOCAL_PSQL_OPTIONS postgres 
# backup the current local db
psql $LOCAL_PSQL_OPTIONS postgres "ALTER DATABASE current_db_name RENAME TO current_db_name_backup;"
# rename the tmp db to the expected local database name
psql $LOCAL_PSQL_OPTIONS postgres "ALTER DATABASE my_tmp_db RENAME TO current_db_name;"

# if you trust this script, uncomment this line that clean old backups
# psql $LOCAL_PSQL_OPTIONS postgres "DROP DATABASE current_db_name_backup;"

Hope that helps.

If you could had any ssh access, you could gzip the sql dump before transfert but for now this is your only choice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.