migrate icon indicating copy to clipboard operation
migrate copied to clipboard

Migrations doesn't work with Postgres 10

Open apremalal opened this issue 8 years ago • 2 comments

Hello Devs,

I'm trying to execute the postgres migration sample code below.

package main

import (
	"database/sql"
	_ "github.com/lib/pq"
	"github.com/mattes/migrate"
	"github.com/mattes/migrate/database/postgres"
	_ "github.com/mattes/migrate/source/file"
	"fmt"
)

func main() {
	connURLP := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
		"postgres", "root", "localhost", "5432", "test_db")
	fmt.Println(connURLP)
	connP, err := sql.Open("postgres", connURLP)
	if err != nil {
		fmt.Println(err.Error())
	}
	driver, err := postgres.WithInstance(connP, &postgres.Config{})
	if err != nil {
		fmt.Println(err.Error())
	}
	m, err := migrate.NewWithDatabaseInstance(
		"file://migrations",
		"postgres", driver)
	if err != nil {
		fmt.Println(err.Error())
	}
	err = m.Steps(20)

	if err != nil {
		fmt.Println(err.Error())
	}
}

with the sql file.

CREATE TABLE books (
  user_id integer,
  name    varchar(40),
  author  varchar(40)
);

It throws (details: pq: syntax error at or near "CREATE"). However the mysql version of the same code works perfectly.

go version go1.9.1 darwin/amd64

apremalal avatar Oct 17 '17 06:10 apremalal

@apremalal Have you tried quoting the table name? I ran into a similar issue and for some reason in Postgres 10 I had to use double quotes with my table names. I really don't understand why but it might work for you.

CtrlC-Root avatar Nov 29 '17 04:11 CtrlC-Root

I had the same problem but it was also occuring with postgres 9 and 8. After a pile of wasted time I ended up cutting my migration file down to nothing and retyping the query, and it succeeded - I'm pretty sure this was because of unprintable characters breaking the parser. Hope this helps somebody.

pschuegr avatar Apr 16 '18 05:04 pschuegr