dotConnect for PostgreSQL Documentation
In This Topic
    Creating Database Objects
    In This Topic

    This tutorial describes how to create tables, stored procedures and other objects at PostgreSQL server.

    In this tutorial:

    Requirements

    In order to create database objects you have to connect to server. This process is described in details in the tutorial Logging onto the server.

    General Information

    Database objects are created using Data Definition Language (DDL), which is a part of SQL. The DDL statements can be executed on server by account that has necessary privileges.

    There are two ways to manipulate a database. You can build DDL statements manually and run them within the psql terminal or component like PgSqlCommand. Another way is to use pgAdmin III - visual shells that provide graphical user interface to manage database. We will discuss the first way. As for pgAdmin III documentation, please refer to https://www.pgadmin.org/docs/.

    Using psql

    1. Launch the psql terminal and authorize yourself.

    2. Type:

      CREATE TABLE dept (
        deptno INT PRIMARY KEY,
        dname VARCHAR(14),
        loc VARCHAR(13)
      );

      Press ENTER. This will create first of the tables we'll use for tutorial purposes.

    3. Run the following query:

      CREATE TABLE emp (
        empno INT PRIMARY KEY,
        ename VARCHAR(10),
        job VARCHAR(9),
        mgr INT,
        hiredate DATE,
        sal FLOAT,
        comm FLOAT,
        deptno INT REFERENCES dept
      );

      This is another table we'll use.

    4. These two tables are enough to demonstrate basic functionality. Now you can type \q to exit the psql terminal.

    Additional Information

    Actually there are lots of ways to create tables on server. Any tool or component that is capable of running a SQL query, can be used to manage database objects. For example, PgSqlCommand suits fine for creating objects one by one, while PgSqlScript is designed for executing series of DDL/DML statements. For information on DDL statements syntax refer to PostgreSQL documentation.

    See Also

    Getting Started  | PgSqlCommand Class  | PgSqlScript Class