Skip to content
Advertisement

Postgres Import from different table

I’m still fairly new to postgres. I have a table named: university_table with fields: name, nationality, abbreviation, adjective, person.

screenshot of table columns

I found this sql query to insert data from: https://stackoverflow.com/a/21759321/9469766 Snippet of query below. How can alter the query to insert these values into my university_country table — Create and load Nationality Table – English

Advertisement

Answer

To convert T-SQL to be compatible with Postres’ SQL dialect you can use the following steps.

  1. Remove all square brackets (they are illegal in SQL identifiers). If you have identifiers that require them use double quotes " but I would highly recommend to avoid quoted identifiers completely (so never use " in SQL)
  2. Remove all GO statements and end the statements with ; (Something that is recommended for SQL Server as well)
  3. Remove the [dbo]. schema prefix if you didn’t create one in Postgres (you typically don’t)
  4. Remove the ON [Primary] option it’s not needed in Postgres (the equivalent would be to define a tablespace, but that’s hardly ever needed in Postgres)
  5. There is no IF in SQL (or Postgres), to conditionally drop a table use DROP TABLE IF EXISTS ....
  6. There are no clustered indexes in Postgres, so just make that a regular index and remove all the options that are introduced by the WITH keyword.
  7. Comments on tables are defined through comment on, not by calling a stored procedure
  8. identity(x,y) needs to be replaced with the standard SQL generated always as identity
  9. There is no nvarchar type, just make everything varchar and make sure your database was created with an encoding that can store multi-byte characters (by default it’s UTF-8, so that should be fine)
  10. Not required, but: it’s highly recommended to use snake_case identifiers, rather than CamelCase in Postgres

Putting that all together the script should be something like this:

I am a bit surprised that there is no primary key defined. You probably want to add:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement