Commands to restore db from dump using psql inside Docker postgresql container

1) docker-compose run app bundle exec rake db:create (Create Database)

2) Docker ps (get container name)

3) docker exec -it database_container_name_1 bash

4) ls (to see if you have backups directory)

5) mkdir backup ( create backup directory if it doesnt exist)

6) docker cp /Users/ganeshprasad/Downloads/db_copy.sql database_container_name_1:/backup (Copy backup file to backup folder inside container)

7) docker exec -it database_container_name_1 bash

8) psql -U db_owner -d db_name -W < backup/db_copy.sql (Restore the db using psql)

9) sudo rm backup/db_copy.sql (to delete backup)

--

--

To decode a pdf

attachment = response_xml.search(‘Attachment’).first.text
decode_base64_content = Base64.decode64(attachment)
filename = Rails.root.join(‘tmp’, ‘orders’, “#{order.id}-#{SecureRandom.hex(3)}.pdf”)

ensure_tmp_dir_exists_for filename

File.open(filename, ‘wb’) do |f|
f.write(decode_base64_content)
end
filepath = File.open(filename)
File.unlink filename

def ensure_tmp_dir_exists_for(filename)
Dir.mkdir File.dirname(filename)
rescue Errno::EEXIST
end

To encode an image

require "base64"

base64_image = File.open("doc/image.png", "rb") do |file|
Base64.strict_encode64(file.read)
end

--

--