How restore a PostgreSQL table from *.sql using pg_dump or psql?

54,051

Solution 1

Just connect to database with psql and run \i /path/to/filename.sql.

Solution 2

psql --username yourusername --dbname yourdatabasename -f yourfile.sql

as clarified here. Depending on configuration, may ask for your password.

If it is a newly installed database engine with no your database yet, use postgres for the database name and try to omit the username part (new installation should normally grant the full access to the current user who installed it).

If you still can’t login, edit temporarily pg_hba.conf wherever it could be in your installation and temporarily set the localhost to trusted. Then you can specify postgres both as username and as the database name.

Don’t forget to revert pg_hba.conf changes when done.

Solution 3

psql dbname < /path/to/dump.sql

You may even modify your dump on the fly if required:

sed 's/OWNER TO olduser/OWNER TO newuser/g' <  /path/to/dump.sql | psql dbname

Solution 4

psql -U postgres -d doctor_dev < /home/ravi/mydevelopment
Share:
54,051

Related videos on Youtube

NiLL
Author by

NiLL

web developer ^_^

Updated on January 21, 2020

Comments

  • NiLL
    NiLL over 4 years

    I need to restore a big table (250mb) in PostgreSQL database in console tool. How I can do this using ps_dump or psql?

    • Kungi
      Kungi over 12 years
      you can get the help for all commands with \h in the psql shell
  • bad_keypoints
    bad_keypoints about 9 years
    This was highly useful. I needed something that could be executed from a script. Thanks!
  • Eric Leschinski
    Eric Leschinski over 7 years
    This isn't an answer. This code only sends unknown sql statements from file to the postgresql database.
  • eXPRESS
    eXPRESS over 5 years
    This worked while using psql like: psql -d newdb -f db.sql did nothing. Anyone have a clue why? BTW thanks very much.
  • volume one
    volume one about 2 years
    this restores the whole DB not a single table