-1

I am working in rails application and i have deployed my application into docker.My local database having around 100 tables with data.I want to push those table data into my docker postgresql db.How to Copy Local database table data into docker postgtesql database? I have added docker ps logs below.

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

  Data page checksums are disabled.

 fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
 running bootstrap script ... ok
 performing post-bootstrap initialization ... ok
 syncing data to disk ... ok

 Success. You can now start the database server using:

pg_ctl -D /var/lib/postgresql/data -l logfile start


 WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
****************************************************
WARNING: No password has been set for the database.
     This will allow anyone with access to the
     Postgres port to access your database. In
     Docker's default configuration, this is
     effectively any other container on the same
     system.

     Use "-e POSTGRES_PASSWORD=password" to set
     it in "docker run".
 ****************************************************
 waiting for server to start....2019-03-15 12:22:41.584 UTC [42] LOG:  
listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
 2019-03-15 12:22:41.600 UTC [43] LOG:  database system was shut down at 
 2019- 
  03-15 12:22:40 UTC
 2019-03-15 12:22:41.609 UTC [42] LOG:  database system is ready to accept 
 connections
 done
 server started

  /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

 waiting for server to shut down...2019-03-15 12:22:41.674 UTC [42] LOG:  
 received fast shutdown request
.2019-03-15 12:22:41.677 UTC [42] LOG:  aborting any active transactions
2019-03-15 12:22:41.680 UTC [42] LOG:  background worker "logical replication 
launcher" (PID 49) exited with exit code 1
2019-03-15 12:22:41.680 UTC [44] LOG:  shutting down
2019-03-15 12:22:41.700 UTC [42] LOG:  database system is shut down
done server stopped

 PostgreSQL init process complete; ready for start up. 

2019-03-15 12:22:41.788 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", 
port 5432
2019-03-15 12:22:41.788 UTC [1] LOG:  listening on IPv6 address "::", port 
5432
2019-03-15 12:22:41.792 UTC [1] LOG:  listening on Unix socket 
"/var/run/postgresql/.s.PGSQL.5432"
2019-03-15 12:22:41.807 UTC [51] LOG:  database system was shut down at 
2019- 
03-15 12:22:41 UTC
2019-03-15 12:22:41.812 UTC [1] LOG:  database system is ready to accept 
connections
2019-03-15 12:23:41.118 UTC [81] FATAL:  database "MyAPP" does not exist
Viktor
  • 2,623
  • 3
  • 19
  • 28
user1984
  • 13
  • 2
  • 11
  • create what's called a pg_dump from your local database and upload that into your docker pg database. Google pg_dump. – BenKoshy Mar 15 '19 at 04:10
  • I did the same for influxdb. I have created docker volume at the same path where default database files are stored and then attached the local DB path to Docker container – Mayur Mar 15 '19 at 06:19
  • Ok thanks.Can you share steps? – user1984 Mar 15 '19 at 06:35

1 Answers1

1

You can create a dump from your local/remote postgres using the foloowing command:

pg_dump dbname > dbname.sql

Next you will need to mount this file dbname.sql under the following directory in your container maybe through docker-compose or docker run -v:

/docker-entrypoint-initdb.d/

When the container start it will restore the file into a database as you can see in docker-entrypoint.sh for the postgres image. It has the ability to run sql, sql.gz, sh files.

So for example lets say you have dump.sql inside /home/foo/database_files and you need to restore this dump.sql into a container then you can run the following command:

dokcer run -v /home/foo/database_files:/docker-entrypoint-initdb.d/ -d --name new_postgres_container postgres

Then run docker ps to ensure that your new container is running and you may also check the logs using docker logs new_postgres_container

Alternatively, you can mount the local postgres path directory into your container while creating it using -v option that will be passed to docker run command or add it under docker-compose.yml

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/190130/discussion-on-answer-by-mostafa-hussein-copy-local-database-table-data-into-dock). – Samuel Liew Mar 16 '19 at 08:58
  • thanks for your help.Using below command i am able to copy data. – user1984 Mar 20 '19 at 06:16
  • .docker cp /Users/admin/git/generic/myapp/dump.sql myapp_db_1:/docker-entrypoint-initdb.d/dump.sql docker exec -u postgres myapp_db_1 psql myapp postgres -f docker-entrypoint-initdb.d/dump.sql – user1984 Mar 20 '19 at 06:16