0

In a Python2.7 script, the following gave me an error, I can't figure out why:

import psycopg2
conn = psycopg2.connect("dbname=mydb user=username password=password")
curs = conn.cursor()
curs.execute("CREATE TABLE newtable;")

The error looks like:

Traceback (most recent call last):

  File "<ipython-input-17-f4ba0186c40c>", line 1, in <module>
    curs.execute("CREATE TABLE newtable;")

ProgrammingError: syntax error at or near ";"
LINE 1: CREATE TABLE newtable;

Any SELECT statement works perfectly well on the other hand. For example:

curs.execute("SELECT * FROM table1 LIMIT 0;")

works like a charm.

1 Answer 1

1

CREATE TABLE newtable; is not the correct syntax to create a new table. You need to define some columns.

CREATE TABLE newtable (
    foo INTEGER,
    bar TEXT
);

See the CREATE TABLE docs for more info.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, pretty annoying... Is it possible to easily copy the "structure" of another table and adding only a few new columns instead of writing down tens of columns names and data types?
This is an example for copying the structure from another table; CREATE TABLE newtable AS SELECT * FROM table1 WHERE 1=0;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.