How Do You Create a Table inside a Specific Postgres Database?

Problem scenario
You want to create a table in a specific Postgres database.  You want to use a command line interface (not a GUI) for this task.  (The GUI version of these directions can be found here.)  What are the SQL commands to do this?

This assumes you have a role (a username and password) to connect to the Postgres database with permissions to create tables.

1.  Connect to the Postgres instance (e.g., either through a web front-end or the command line interface).  If you are unsure on how to do this, see this link. If you use an desktop application to connect to the instance, skip step #2.

2.  Connect to the database using a command like this:
\c nameofdatabase \\where nameofdatabase is the name of the database you want

3.  Run a SQL [DDL] command like this (where cool is the name of the table and column1 is the name of the first column in the table):  create table cool(column1 int);

If you are not sure what database you are connected to, run this command:
SELECT current_database();

The database you connect to with a GUI application is chosen at the time you configure the connection.

Leave a comment

Your email address will not be published. Required fields are marked *