Create a Postgres Table on ObjectRocket

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re getting started with a Postgres instance on ObjectRocket, one of the first tasks you’ll probably need to do is create a table. Fortunately, the process is a very simple one. In this article, we’ll show you how to create a Postgres table on ObjectRocket, providing detailed instructions for each step of the task.

Prerequisites

A few key prerequisites need to be in place before proceeding with this tutorial. First, the following software must be installed and configured:

  • PostgreSQL 11

  • You must also create an instance of Postgres for your ObjectRocket account. You can do this using ObjectRocket’s Mission Control panel.

Connect Psql to Postgres Server on ObjectRocket

In order to create a database and a corresponding table, we’ll need to connect to our Postgres server instance on ObjectRocket.

The connection details can be found at the “CONNECT” tab on the ObjectRocket Mission Control panel. Here’s how you can locate those details:

  • First, click on the VIEW MORE DETAILS button on the lower right corner of the panel.

  • Then, select the “CONNECT” tab.

  • Finally, we connect to the Postgres server on ObjectRocket using the following command:
1
psql -h ingress.w98sujpz.launchpad.objectrocket.cloud -p 4149 -U pguser -d PostgreSQL
  • The psql utility will prompt us for the user password to connect to the server. Once we enter it, we’ll be connected to Postgres.

Creating a Postgres Table

Now that we were able to connect to Postgres, we can turn our attention to the task of creating a table. To create a Postgres table, we use the CREATE TABLE SQL statement. The basic syntax of this statement is shown below:

1
2
3
CREATE TABLE <tableName> (
    columnName TYPE columnConstraint,
);

In the above statement, we first call the CREATE TABLE keyword, which instructs the database system to create a new database table. Next, we list the column names and their corresponding data types and column constraints. We can specify multiple columns by separating them with commas.

Creating PostgreSQL Table Example

In this section, we’ll create a PostgreSQL table named personnel with the following details:

  • id: PRIMARY KEY NOT NULL
  • name: VARCHAR(50) NOT NULL
  • lastname: VARCHAR(50) NOT NULL
  • age: INTEGER

Shown below is the CREATE TABLE statement that we will use to create the personnel table:

1
2
3
4
5
6
CREATE TABLE personnel(
    id SERIAL PRIMARY KEY NOT NULL,
    name VARCHAR(50) NOT NULL,
    lastname VARCHAR(50) NOT NULL,
    age INTEGER
);

Note that were able to create multiple columns that were separated by commas.

When we execute the above statement, we’ll see a response like this if no error occurred:

1
CREATE TABLE

To verify that our table was successfully created, we can use the \d command in psql. We should see output that looks like the following:

1
2
3
4
5
6
7
PostgreSQL=> \d
               List of relations
 Schema |       Name       |   Type   | Owner
--------+------------------+----------+--------
 public | personnel        | table    | pguser
 public | personnel_id_seq | sequence | pguser
(2 rows)

We can see that our personnel table was created, although this output doesn’t reveal any details about the table. To view the table details and the corresponding columns, we use the psql command \d followed by the name of the table. The result should look like this:

1
2
3
4
5
6
7
8
9
10
PostgreSQL=> \d personnel
                                    Table "public.personnel"
  Column  |         Type          | Collation | Nullable |                Default
----------+-----------------------+-----------+----------+---------------------------------------
 id       | integer               |           | not null | nextval('personnel_id_seq'::regclass)
 name     | character varying(50) |           | not null |
 lastname | character varying(50) |           | not null |
 age      | integer               |           |          |
Indexes:
    "personnel_pkey" PRIMARY KEY, btree (id)

We can see that our table was created successfully.

Conclusion

Creating a table is a fundamental task in database administration, so it’s important to know how to complete the process correctly. When you work with a Postgres instance on ObjectRocket, it’s easy to create any tables you need. In this article, we showed you how to create a Postgres table on ObjectRocket and provided step-by-step instructions for the entire process. With this tutorial to guide you, you’ll be ready to create tables on your own Postgres instance.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.