Search code examples
postgresqlpostgresql-12

Import .sql file to a specific schema (not in public schema) PostgreSQL 12


sudo psql -f sample-schema.sql -U postgres -p 5432 -h my ipaddress -d dbname

How to specify into what schema the data go?


Solution

  • If the SQL statements in the script are not schema qualified, you can set the search path:

    psql -U postgres -p 5432 -h my ipaddress -d dbname \
      -c 'SET search_path = target_schema' -f sample-schema.sql
    

    If the SQL statements in the script are qualified with the public schema, there is little you can do. You could temporarily rename the public schema, execute the script, then rename the schemas again.