Working with Postgres SQL

  • Connect to the server
-> psql -h mydbserver -U myuser -d mydatabase
-> or: sudo -u postgres psql

-h host: Specifies the hostname or IP address of the PostgreSQL server.   

-p port: Specifies the port number (default is 5432).   

-U username: Specifies the username to connect with.

-d database: Specifies the database to connect to.1

  • To see all databases
-> \l
  • Working with a database
-> \c <db name>
  • To see all tables
-> \dt
  • Describe a table's structure
-> \d table_name
  • \ds: List sequences.
  • \df: List functions.
  • \dv: List views.
  • \du: List users.
  • To change password
-> ALTER USER user_name WITH PASSWORD 'new_password';
  • Rename a db
-> ALTER DATABASE <OLD_NAME> RENAME TO <NEW_NAME>;
  • Dump a database
-> pg_dump -h localhost -U huan -d easyweb_production -s > ea.dump
Back to posts