Macbernie
Macbernie

Reputation: 1323

PostGreSQL CREATE TABLE with PDO PHP

I try to do a simple create table in my PostgreSQL datatable, by executing the createTables function bellow:

    public function createTables() {
        try {
            $db = new PDO('pgsql:host='.$this->PARAM_hote.';port='.$this->PARAM_port.';dbname='.$this->PARAM_nom_bd.';user='.$this->PARAM_utilisateur.';password='.$this->PARAM_mot_passe);

            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $sql ='CREATE TABLE IF NOT EXISTS test (id INT(11) AUTO_INCREMENT PRIMARY KEY, prename VARCHAR(50) NOT NULL);';
            $db->exec($sql);
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }

But I still have:

testSQLSTATE[42601]: Syntax error: 7 ERREUR: erreur de syntaxe sur ou près de « ( » LINE 1: CREATE TABLE IF NOT EXISTS test (id INT(11) AUTO_INCREMENT P... ^ 

And I don't know why ?...

Thanks for help

Upvotes: 1

Views: 2000

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125244

There is no autoincrement in postgresql. In instead use serial. And the integer type is always 4 bytes.

test (id serial PRIMARY KEY,

The serial type implies integer with a not null constraint and an attached sequence.

http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

Upvotes: 5

Related Questions