Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i want to create a simple table inside a database in postgresql. From the Documentation i have CREATE TABLE will create a new, initially empty table in the current database. The table will be owned by the user issuing the command. With this command

CREATE TABLE *table_name*;

I thought i get a new empty table.But psql throws ERROR: syntax error at or near ";". When i user an empty argument list like:

CREATE TABLE *table_name*();

psql tells me that the table was created through

postgres=# create table *table_name*();
CREATE TABLE

But \l shows is not showing the newly created table. And its also not possible to login with psql -d table_name -U user_name. Can anyone help?

share|improve this question
    
Do you really need a table with no columns? One usually calls an empty table the one with no records. The one without columns is kind of odd. – mlt Jul 9 '15 at 15:34
    
I am using Postgres together with Rails. I dropped my database in Rails through db:drop to start from scratch. I now wanted to create a new one in postgres and connect through rails to it – theDrifter Jul 9 '15 at 15:38
    
You got to add columns. Or since you use rails, do it with migrations. You'd want to use raw SQL if you had an existing DB you wanted to integrated with. – mlt Jul 9 '15 at 15:39
    
Oh no. I realised now. All i wanted to do was to create a database not a table. -.-. However through Rails i can add/alter tables. But i need a database to store them. I already got a bit suspicious by the -d option in the terminal, which connects me to not the std database. – theDrifter Jul 9 '15 at 15:47
    
rails can create DB for you as well via rake db:create or something. – mlt Jul 9 '15 at 18:35
up vote 0 down vote accepted

You can have a table with no columns, and even with some rows in it:

CREATE TABLE nocolumn (dummy INTEGER NOT NULL PRIMARY KEY)
    ;

INSERT INTO nocolumn(dummy) VALUES (1);

ALTER TABLE nocolumn
        DROP COLUMN dummy;

\d nocolumn

SELECT COUNT(*) FROM nocolumn;

Output:

CREATE TABLE
INSERT 0 1
ALTER TABLE
   Table "tmp.nocolumn"
 Column | Type | Modifiers 
--------+------+-----------

 count 
-------
     1
(1 row)
share|improve this answer

You seem to be confusing the terms database and table

But \l is not showing the newly created table.

Of course \l will not show you that table, because \l will list databases not relations. To see all tables you need to use \d or \dt.

And its also not possible to login with psql -d table_name -U user_name

Of course this is not possible, because the -d parameter is used to specify a database, not a table

share|improve this answer

An hour ago i suggested to add at least one column like this:

create table tab1 (columnname varchar(42) not null)

But this seems to be not necessary as a commentator just told. (I consider to keep the wrong answer here instead of deleting it, to prevent that others suggest the same)

share|improve this answer
    
This is not really an answer, unless you actually add in a code block that shows you how to add columns, at best this is just a sarcastic dig at the op, and you can do this in the comments, without putting it out as an answer – davethecoder Jul 9 '15 at 15:45
    
I just tested to create a columless table with a database client for mixed server types (HeidiSQL) it always prevents generating tables without columns. Anyway I come to no idea of a usefull use of tables containing no colums that enables them to ever carry data in it. But thanks for the downvote.. 2 more an I can earn the peer pressure badge -.- – André Kleinschmidt Jul 9 '15 at 15:49
    
I've added a code block adding a column... I was not aware of the rule that answers on Stackoverflow.SE must contain codeblocks always. Sorry. – André Kleinschmidt Jul 9 '15 at 15:54
    
There is a rule that an answer, is an answer. If you ask a question, Do people just answer with " You are doing it wrong " answers? – davethecoder Jul 9 '15 at 15:56
    
also I did not downvote, If i downvote, I say, and I don't really downvote unless it is really terrible – davethecoder Jul 9 '15 at 15:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.