Question

I'm trying to restore a database from backup but I can't connect to postgresql.

namespace :db do
  task import: :environment do
  import_path = "~/backups"
  sql_file = "PostgreSQL.sql"
  database_config = Rails.configuration.database_configuration[Rails.env]

  system "psql --username=#{database_config['username']} -no-password # {database_config['database']} < #{import_path}/#{sql_file}"
  end
end

I tried changing the pg_hba.conf file (peer to md5). In the console I tried the same thing with the super user postgres, but it still fails.

BTW, does anyone know a better way to restore a database? I used the backup gem.

EDIT: I restarted the postgresql server and the passed the authentication. But, didn't restored the db. I reverted the changes in the file and just added -h localhost to the psql command. The database restores now. The only errors I get now are:

must be owner of extension plpgsql //and
no privileges could be revoked for "public"
Was it helpful?

Solution

  1. after change pg_hba.conf, you shold reload or send a SIGHUP signal to postmaster pid. so that change applyed.
  2. why not use psql -f to execute the backup sql file?
  3. or you can use pg_dump backup and pg_restore restore. or copy command backup and restore. LIKE :

    digoal=# copy tbl_join_1 to '/home/pg93/tbl_join_1.dmp';
    COPY 10
    digoal=# delete from tbl_join_1;
    DELETE 10
    digoal=# copy tbl_join_1 from '/home/pg93/tbl_join_1.dmp';
    COPY 10
    

    OR

    pg93@db-172-16-3-150-> pg_dump -f ./tbl_join_1.dmp -t tbl_join_1
    pg93@db-172-16-3-150-> psql
    psql (9.3.3)
    Type "help" for help.
    
    digoal=# drop table tbl_join_1;
    DROP TABLE
    digoal=# \q
    pg93@db-172-16-3-150-> psql -f ./tbl_join_1.dmp 
    SET
    SET
    SET
    SET
    SET
    SET
    SET
    SET
    SET
    CREATE TABLE
    ALTER TABLE
    ALTER TABLE
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top