Youssef Samir

Get in touch

PostgreSQL

Discover PostgreSQL, a powerful, open-source object-relational database system known for its reliability and feature set.

Connecting to PostgreSQL

  • Connect to a PostgreSQL database:

psql -U username -d database_name
  • Connect as a different user and specify a database:
    psql -U username -d database_name -h hostname -p port
    

Managing Databases

  • List all databases:
    \l
    
  • Create a new database:
    CREATE DATABASE database_name;
    
  • Drop a database:
    DROP DATABASE database_name;
    
  • Connect to a database:
    \c database_name
    

Managing Tables

  • List all tables in the current database:
    \dt
    
  • Describe the structure of a table:
    \d table_name
    
  • Drop a table:
    DROP TABLE table_name;
    

Managing Users

  • List all users/roles:
    \du
    
  • Create a new user:
    CREATE USER username WITH PASSWORD 'password';
    
  • Grant privileges to a user:
    GRANT ALL PRIVILEGES ON DATABASE database_name TO username;
    
  • Drop a user:
    DROP USER username;
    

Miscellaneous Commands

  • Show current database and user:
    \conninfo
    
  • Show PostgreSQL version:
    \version
    
  • Exit the PostgreSQL terminal:
    \q
    
  • Show available commands:
    \?
    
  • Show help on a specific command:
    \h command_name
    
  • Run a SQL script file:
    \i /path/to/script.sql