Restore One Table with psql from a pg_dump PostgreSQL Backup

Backing up and restoring PostgreSQL is fairly easy using pg_dump and psql. One more complicated scenario I have run into is doing a complete database backup with pg_dump and at some point down the road needing to just split out one table and restore it. This can be a pain because of how the pg_dump organizes the .sql file it outputs. I have found the best way to do this is to use pg_dump to backup one table to understand the format of what is needed for a restore. Then search the .sql file that was output from the full pg_dump output and split out the necessary data to restore into one table. Below I provide some examples as well as syntax to use for backing up and/or restoring PostgreSQL using pg_dump and psql.

First lets look at the syntax required to backup a PostgreSQL database using pg_dump which I have done below. The first command will backup the entire database and output to a SQL file in the default format.


  1. [root@server backups]# pg_dump -U postgres dbname > data-dump.sql

Some people understand INSERT commands better so the second command shows how the -d switch can be added to output INSERT commands instead of COPY commands.


  1. [root@server backups]# pg_dump -U postgres -d dbname> data-dump.sql

The third example below shows dumping the data only using the -a switch.


  1. [root@server backups]# pg_dump -U postgres -a dbname > data-dump.sql

The last example using pg_dump shows the -a switch for data only, the -d switch for INSERT commands, and the -t switch for dumping only one tables data.


  1. [root@server backups]# pg_dump -U postgres -a -d -t tablename dbname > data-dump.sql

The above examples provide you numerous examples of how you could be backing up your PostgreSQL database. The point behind this article though is to demonstrate how you could split out one tables data from a backup file that was created using pg_dump and backing up an entire database. First lets look at how you would restore an entire database which can be done using the syntax below.


  1. [root@server backups]# psql -U postgres dbname < data-dump.sql
  2. &#91;/sourcecode]
  4. Now if you only needed to restore one tables data you would first need to create a file with only that tables data instead of data for the entire database. I recommend issuing the pg_dump command to backup only one tables data so it can be understood what format the data needs to be in. Below I show an example of the syntax used to dump one tables data using the default COPY command format followed by what the contents of that file would look like.
  5. &#91;sourcecode language="bash"]
  6. &#91;root@server backups]# pg_dump -U postgres -a -t tablename dbname > data-dump.sql

Now what the contents of the data-dump.sql file might look like.


  1. --
  2. -- PostgreSQL database dump
  3. --
  5. SET client_encoding = 'UTF8';
  6. SET standard_conforming_strings = off;
  7. SET check_function_bodies = false;
  8. SET client_min_messages = warning;
  9. SET escape_string_warning = off;
  11. SET search_path = public, pg_catalog;
  13. --
  14. -- Name: user_options_data_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
  15. --
  17. SELECT pg_catalog.setval('user_options_data_id_seq', 39, true);
  19. --
  20. -- Data for Name: user_options_data; Type: TABLE DATA; Schema: public; Owner: postgres
  21. --
  23. COPY user_options_data (id, user_data_id, removed, active, phone_active, email_active, text_active, "position", relationship) FROM stdin;
  24. 18 55 f t f f f 2 \N
  25. 6 17 f t f f f 2 \N
  26. 20 58 f t f f f 1 \N
  27. 1 4 f t f f f 1 \N
  28. 2 1 f t t t f 1
  29. 4 15 f t f t f 1 \N
  30. 7 29 t f t f t 0 \N
  31. 9 35 f t f f f 1 \N
  32. 10 38 f t f f f 1 \N
  33. 11 40 f t f f f 1 \N
  34. 12 42 f t f f t 1 \N
  35. 13 45 f t f f f 1 \N
  36. 23 64 f t f t t 1 \N
  37. 21 60 f t f t f 1 \N
  38. 24 66 f t f t f 1 \N
  39. 15 46 f t f f f 0 \N
  40. 22 61 f t f t f 0 \N
  41. 17 53 t t f f f 0 \N
  42. 25 68 f f f f f 1 \N
  43. 26 72 f f f f f 1 \N
  44. 14 44 f f f f f 0 \N
  45. 16 49 f t f f f 0 \N
  46. 37 99 f t f f f 1 \N
  47. 38 100 t t f t t 0 \N
  48. 27 73 f f f f f 1 \N
  49. 36 97 t t f t t 0 \N
  50. 39 102 f t f t t 1 \N
  51. 30 77 f t f f f 1 \N
  52. 31 80 t f f f f 0 \N
  53. 33 89 f t f f f 1 \N
  54. 29 76 f f f f f 1 \N
  55. 28 75 t f f t f 0 \N
  56. 32 87 t f f f f 0 \N
  57. 34 92 f t f f f 1 \N
  58. 8 32 t t t t t 0 \N
  59. 35 96 t t f f f 0 \N
  60. 19 56 f t f f f 1 \N
  61. 5 16 f f f f f 0 \N
  62. 3 10 f t f t f 2 \N
  63. \.
  65. --
  66. -- PostgreSQL database dump complete
  67. --

This is a great example of what needs to be obtained and split from the backup of the entire database. You will need to use the less, cat, or similar Linux command to locate the data you need within the file. You could also use vi and search for the table name within the file. Using the table only example you will understand what commands are necessary to have what is needed to restore the data into the PostgreSQL database. You will need to add the SET commands that are displayed at the top of the table data only file you created as an example as those will not be located in the complete database backup searching by table name. Once you have created the new file which again should mirror the example you created for dumping only one tables data then you can import using psql as shown below.

[root@server backups]# psql -U postgres dbname < new-one-tables-data.sql

That is all you need to do. If you do receive an error during the import study any differences between the example table data SQL file and the new SQL file you have created. While all of the above steps seem like a bit of a pain it is nice to know it is not to complex to restore only one tables data into PostgreSQL.