title: pg_restore not restoring a certain table?
body: <p>In a Django project, I have a model <code>Question</code> in the <code>lucy_web</code> app, but the corresponding <code>lucy_web_question</code> table does not exist, as seen from a <code>\dt</code> command in the database shell:</p>
  
  <pre><code>(lucy-web-CVxkrCFK) bash-3.2$ python manage.py dbshell
  psql (10.4)
  Type "help" for help.
  
  lucy=&gt; \dt
                      List of relations
   Schema |             Name             | Type  |  Owner  
  --------+------------------------------+-------+---------
   public | auditlog_logentry            | table | lucyapp
   public | auth_group                   | table | lucyapp
   public | auth_group_permissions       | table | lucyapp
   public | auth_permission              | table | lucyapp
   public | auth_user                    | table | lucyapp
   public | auth_user_groups             | table | lucyapp
   public | auth_user_user_permissions   | table | lucyapp
   public | defender_accessattempt       | table | lucyapp
   public | django_admin_log             | table | lucyapp
   public | django_content_type          | table | lucyapp
   public | django_migrations            | table | lucyapp
   public | django_session               | table | lucyapp
   public | lucy_web_checkin             | table | lucyapp
   public | lucy_web_checkintype         | table | lucyapp
   public | lucy_web_company             | table | lucyapp
   public | lucy_web_expert              | table | lucyapp
   public | lucy_web_expertsessiontype   | table | lucyapp
   public | lucy_web_family              | table | lucyapp
   public | lucy_web_lucyguide           | table | lucyapp
   public | lucy_web_notification        | table | lucyapp
   public | lucy_web_package             | table | lucyapp
   public | lucy_web_packagesessiontype  | table | lucyapp
   public | lucy_web_preactivationfamily | table | lucyapp
   public | lucy_web_profile             | table | lucyapp
   public | lucy_web_questionanswer      | table | lucyapp
   public | lucy_web_questioncategory    | table | lucyapp
   public | lucy_web_session             | table | lucyapp
   public | lucy_web_sessioncategory     | table | lucyapp
   public | lucy_web_sessiontype         | table | lucyapp
   public | lucy_web_userapn             | table | lucyapp
   public | oauth2_provider_accesstoken  | table | lucyapp
   public | oauth2_provider_application  | table | lucyapp
   public | oauth2_provider_grant        | table | lucyapp
   public | oauth2_provider_refreshtoken | table | lucyapp
   public | otp_static_staticdevice      | table | lucyapp
   public | otp_static_statictoken       | table | lucyapp
   public | otp_totp_totpdevice          | table | lucyapp
   public | two_factor_phonedevice       | table | lucyapp
  (38 rows)
  </code></pre>
  
  <p>We also have a staging environment, deployed on Aptible, which does appear to have these tables. Using the <a href="https://www.aptible.com/documentation/enclave/cli/db-tunnel.html" rel="nofollow noreferrer">Aptible CLI to create a database tunnel</a>, if I <code>psql &lt;connection_url&gt;</code> and <code>dt</code> I do see the <code>lucy_web_question</code> table:</p>
  
  <pre><code>db=# \dt
                      List of relations
   Schema |             Name             | Type  |  Owner  
  --------+------------------------------+-------+---------
   public | auditlog_logentry            | table | aptible
   public | auth_group                   | table | aptible
   public | auth_group_permissions       | table | aptible
   public | auth_permission              | table | aptible
   public | auth_user                    | table | aptible
   public | auth_user_groups             | table | aptible
   public | auth_user_user_permissions   | table | aptible
   public | defender_accessattempt       | table | aptible
   public | django_admin_log             | table | aptible
   public | django_content_type          | table | aptible
   public | django_migrations            | table | aptible
   public | django_session               | table | aptible
   public | lucy_web_checkin             | table | aptible
   public | lucy_web_checkintype         | table | aptible
   public | lucy_web_company             | table | aptible
   public | lucy_web_expert              | table | aptible
   public | lucy_web_expertsessiontype   | table | aptible
   public | lucy_web_family              | table | aptible
   public | lucy_web_lucyguide           | table | aptible
   public | lucy_web_notification        | table | aptible
   public | lucy_web_package             | table | aptible
   public | lucy_web_packagesessiontype  | table | aptible
   public | lucy_web_preactivationfamily | table | aptible
   public | lucy_web_profile             | table | aptible
   public | lucy_web_question            | table | aptible
   public | lucy_web_questionanswer      | table | aptible
   public | lucy_web_questioncategory    | table | aptible
   public | lucy_web_questionprompt      | table | aptible
   public | lucy_web_session             | table | aptible
   public | lucy_web_sessioncategory     | table | aptible
   public | lucy_web_sessiontype         | table | aptible
   public | lucy_web_userapn             | table | aptible
   public | oauth2_provider_accesstoken  | table | aptible
   public | oauth2_provider_application  | table | aptible
   public | oauth2_provider_grant        | table | aptible
   public | oauth2_provider_refreshtoken | table | aptible
   public | otp_static_staticdevice      | table | aptible
   public | otp_static_statictoken       | table | aptible
   public | otp_totp_totpdevice          | table | aptible
   public | two_factor_phonedevice       | table | aptible
  (40 rows)
  </code></pre>
  
  <p>Because the data on these test environments is not important, I'd like to <code>pg_dump</code> the Aptible database and <code>pg_restore</code> it on my local machine.</p>
  
  <p>My local <code>DATABASE_URL</code> is <code>postgres://lucyapp:&lt;my_password&gt;@localhost/lucy</code>, so firstly, I did a <code>pg_dump</code> with <code>--format=custom</code> and specifying a <code>--file</code> as follows:</p>
  
  <pre><code>Kurts-MacBook-Pro-2:lucy2 kurtpeek$ touch staging_db_12_July.dump
  Kurts-MacBook-Pro-2:lucy2 kurtpeek$ pg_dump postgresql://aptible:&lt;some_aptible_hash&gt;@localhost.aptible.in:62288/db --format=custom --file=staging_db_12_July.dump
  Kurts-MacBook-Pro-2:lucy2 kurtpeek$ ls -lhtr | tail -1
  -rw-r--r--   1 kurtpeek  staff   1.5M Jul 12 18:09 staging_db_12_July.dump
  </code></pre>
  
  <p>This results in a 1.5Mb <code>.dump</code> file, which I tried to restore from using <code>pg_restore</code> with the <code>--no-owner</code> option and <code>--role=lucyapp</code> (in order change the owner from <code>aptible</code> to <code>lucyapp</code>). However, this gives rise to a large number of 'already exists' errors, of which one is shown below:</p>
  
  <pre><code>Kurts-MacBook-Pro-2:lucy2 kurtpeek$ pg_restore staging_db_12_July.dump --dbname=lucy --no-owner --role=lucyapp
  pg_restore: [archiver (db)] Error while PROCESSING TOC:
  pg_restore: [archiver (db)] Error from TOC entry 3522; 0 0 COMMENT EXTENSION plpgsql 
  pg_restore: [archiver (db)] could not execute query: ERROR:  must be owner of extension plpgsql
      Command was: COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
  
  
  
  pg_restore: [archiver (db)] Error from TOC entry 2; 3079 16392 EXTENSION hstore 
  pg_restore: [archiver (db)] could not execute query: ERROR:  permission denied to create extension "hstore"
  HINT:  Must be superuser to create this extension.
      Command was: CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
  
  
  
  pg_restore: [archiver (db)] Error from TOC entry 3523; 0 0 COMMENT EXTENSION hstore 
  pg_restore: [archiver (db)] could not execute query: ERROR:  extension "hstore" does not exist
      Command was: COMMENT ON EXTENSION hstore IS 'data type for storing sets of (key, value) pairs';
  
  
  
  pg_restore: [archiver (db)] Error from TOC entry 197; 1259 16515 TABLE auditlog_logentry aptible
  pg_restore: [archiver (db)] could not execute query: ERROR:  relation "auditlog_logentry" already exists
      Command was: CREATE TABLE public.auditlog_logentry (
      id integer NOT NULL,
      object_pk character varying(255) NOT NULL,
      object_id bigint,
      object_repr text NOT NULL,
      action smallint NOT NULL,
      changes text NOT NULL,
      "timestamp" timestamp with time zone NOT NULL,
      actor_id integer,
      content_type_id integer NOT NULL,
      remote_addr inet,
      additional_data jsonb,
      CONSTRAINT auditlog_logentry_action_check CHECK ((action &gt;= 0))
  );
  
  
  
  WARNING: errors ignored on restore: 294
  </code></pre>
  
  <p>The problem is that if I <code>\dt</code> again in the <code>python manage.py dbshell</code>, I still don't see the <code>lucy_web_question</code> table.</p>
  
  <p>I've come across this solution, <a href="https://stackoverflow.com/questions/27583744/django-table-doesnt-exist">Django : Table doesn&#39;t exist</a>, for my situation, but in my case the <code>Question</code> model is imported and used as a foreign key in so many places that I thought it would be easier just to restore a database. Why is it not restoring the <code>lucy_web_question</code> table, though?</p>