Skip to content

Instantly share code, notes, and snippets.

@waramity
Last active October 20, 2023 07:31
Show Gist options
  • Save waramity/b16be95ba4ae813873c469c72063e7e4 to your computer and use it in GitHub Desktop.
Save waramity/b16be95ba4ae813873c469c72063e7e4 to your computer and use it in GitHub Desktop.
Postgres Command
# install
brew install postgresql@14
# run server
pg_ctl -D /opt/homebrew/var/postgresql@14 start
# open database
psql <database_name>
# open database with specific user name
psql postgres -U <user_name>
# (inside db)
# create new user with login permission
CREATE ROLE <user_name> WITH LOGIN;
# add create db permission
ALTER ROLE <user_name> CREATEDB;
# list user
\du
# quit
\q
# create database
CREATE DATABASE <database_name>;
# drop db
DROP DATABASE <database name>;
# grant permission to user
GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user_name>;
# list all database
\list
# connect specific database in postgres database
\c <database_name>
# list all table
\dt
# .env for node.js
PG_CONNECTION_STRING=postgres://myuser@localhost/mydatabase
# backup
pg_dump "postgresql://myuser:mypassword@localhost:5432/mydb" > backup.sql
# restore
psql -U <user_name> -d <databaase_name> < <file_name>.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment