DEV Community

Shashank Trivedi
Shashank Trivedi

Posted on • Edited on

Postgresql commands

  • Install postgresql
    brew install postgresql@14

  • Start postgresql as a service
    brew services start postgresql@14

  • Check if postgres is running
    psql --version

  • If you don’t want PostgreSQL to run as a service, you can start it manually:
    pg_ctl -D /opt/homebrew/var/postgres start

  • Stop the service
    brew services stop postgres

  • Restart the service
    brew services restart postgres

  • List all running services
    brew services list

  • Login to postgres
    psql postgres

  • Create User
    CREATE USER app_user WITH PASSWORD app_password;

  • Alter role
    ALTER ROLE app_user CREATE_DB;

  • check all users
    \du

  • Exit using CTRL+D

  • Login using new user
    psql postgres -u app_user

  • create Database
    CREATE DATABASE app_database

  • To check all databases
    \l

  • connect to db
    \connect app_database

  • list all tables
    \dl

  • check table definition
    \d table_name

  • Once you are connected to the database, you can run the SQL script file using the following command
    \i /path/to/your/script.sql

  • you can exit the psql interface by typing
    \q

Once you're connected, you can use the COPY command to import the CSV file. The basic syntax is:
COPY table_name FROM '/path/to/your/file.csv' DELIMITER ',' CSV HEADER;

Top comments (0)