Postgresql – Creating a table with two columns

postgresqltable

I created a database with PostgreSQL, but I am extremely unfamiliar with SQL and databases and am having some issues.

I want to create a table that contains two values:

  • "ID" which is my primary key.
  • And "original_URL" field which will contain URL's with a TEXT type.

So I made this statement, which I think should work

CREATE TABLE ID_URL(
   ID INT PRIMARY KEY     NOT NULL,
   original_URL   TEXT    NOT NULL,
);

However, I've come to an embarrassing stopping point. I created the database via the command line, but I don't know where to put this create statement to create the database.

Where do I run the above code to make it create the table in my database?

Best Answer

When you get a connection to PostgreSQL it is always to a particular database. To access a different database, you must get a new connection.

Using \c in psql closes the old connection and acquires a new one, using the specified database and/or credentials. You get a whole new back-end process and everything.

So lets say the name of your database is mydb

Type this

\c mydb

And than

CREATE TABLE ID_URL( ID INT PRIMARY KEY NOT NULL, original_URL TEXT NOT NULL);

(The surplus comma that would cause a syntax error was removed.)