Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save YoucefMegoura/6b919efecb8672277671f35d49717b80 to your computer and use it in GitHub Desktop.
Save YoucefMegoura/6b919efecb8672277671f35d49717b80 to your computer and use it in GitHub Desktop.

Exporting and importing postgres database


Exporting

Access to wallix server

  • Connect to Wallix X2Go .
  • Connect to server via ssh
# ssh -t [userName]@server
ssh -t userName@10.2.3.118
  • Choose postgres cluster

To connect directly to the cluster

# ssh -t root@local@[clusterName]:[userName]@[server]
ssh -t root@apiz-cluster01-postgresql01:sad@10.2.3.118
# export database in a DMP file with some exluded tables

#pg_dump -h localhost -U [username] --schema=[database] \
#    --exclude-table-data=[table_name] \
#    --no-owner -x -b -f [file.pgsql]  

pg_dump -h localhost -U samvaz --schema=samvaz \
--exclude-table-data=ad_changelog \
--exclude-table-data=ad_archive \
--exclude-table-data=ad_issue \
--exclude-table-data=ad_wf_eventaudit \
--exclude-table-data=ad_wf_activity \
--exclude-table-data=ad_wf_process \
--exclude-table-data=ad_note \
--exclude-table-data=ad_querylog \
--exclude-table-data=ad_pinstance_log \
--exclude-table-data=ad_pinstance_para \
--exclude-table-data=ad_pinstance \
--exclude-table-data=fact_acct \
--exclude-table-data=ad_attachment \
--exclude-table-data=Z_PATCH \
--no-owner -x -b -f samvaz_database.pgsql

Download the file from the server :
```sh
#scp [username@adressIpServer]:[cluster name]:[dmp file path] [local storage path]

scp yme@10.2.3.118:root@local@apiz-cluster01-postgresql01:/home/apiz/peeters_27012022.tar.gz /home/youcefmegoura/Downloads

Importing

To change the the postgres user's password : sudo -u postgres psql -c "ALTER USER postgres PASSWORD '<new-password>';"

With script

  • Clone migrate-pgs project :
git clone https://gitlab.audaxis.com/compiere-audaxis/compiere-tools/migrate-pgs
  • Add execute permissions to all .sh files :
sudo chmod a+x ./*.sh
  • Edit properties.conf :
DB_SYSTEM_USER=postgres
DB_SYSTEM_PASSWORD=postgres
DB_HOST=localhost
DB_PORT=5432
DB_USER=peeters
DB_PASSWORD=peeters
DB_NAME=peeters
OUTPUT_DIR=ora2pg_sql
CORRECTION=peeters_correction
CORRECTION_REPO=correction
ORAFCE_VERSION=3.9
  • Execute create databse script :
./createDB.sh

if you have an error with encoding ^M : with vim tap :set fileformat=unix and save the files :wq (properties.conf, createDB.sh).

  • Import database :
#psql -h [host] -U [user] -d [database] -f [filename.pgsql]
psql -h localhost -U peeters -d peeters -f peeters.pgsql

Docker

Connect to postgres container as root

docker exec -u root --workdir / -ti [container_name]/bin/sh

Copy file in a container

sudo docker cp peeters_database.pgsql [container name]:/data/

Allow access to postgres from other machines :

sudo ufw allow 5432;

Open postgresql.conf file and edit listen_addresses :

listen_addresses = '*'

Open pg_hba.conf file and add following entry at the very end :

host    all             all              0.0.0.0/0                       md5
host    all             all              ::/0                            md5

In generale you find postgres config files in : /etc/postgresql/[pg_version]/main/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment