Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running SQL script through psql gives syntax errors that don't occur in PgAdmin

I have the following script to create a table:

-- Create State table.
DROP TABLE IF EXISTS "State" CASCADE;
CREATE TABLE "State" (
 StateID SERIAL PRIMARY KEY NOT NULL,
 StateName VARCHAR(50)
);

It runs fine in the query tool of PgAdmin. But when I try to run it from the command line using psql:

psql -U postgres -d dbname -f 00101-CreateStateTable.sql

I get a syntax error as shown below.

2: ERROR:  syntax error at or near ""
LINE 1: 
        ^
psql:00101-CreateStateTable.sql:6: NOTICE:  CREATE TABLE will create implicit sequence "State_stateid_seq" for serial column "State.stateid"
psql:00101-CreateStateTable.sql:6: NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "State_pkey" for table "State"
CREATE TABLE

Why do I get a syntax error using psql and not with pgAdmin?

like image 432
Peter Avatar asked Mar 30 '10 18:03

Peter


1 Answers

Run your file 00101-CreateStateTable.sql through a hex dumper. I'll bet you have a UTF-16 marker at the beginning of the file (before the "--" comment characters).

like image 77
Matthew Wood Avatar answered Nov 03 '22 20:11

Matthew Wood