Postgres Command Line

psql: FATAL: password authentication failed for user error while trying to access psql
Ans
psql by default will use the same username as your OS session is using. You need to first login as postgres or switch to
its user (assuming it exists): su – postgres or explicitly type username for psql psql -U postgres.

Login User :

psql -U postgres

log in with

psql --username=postgres

Backup Restore :

Note : Before restore db we should have the database

"postgres=# CREATE DATABASE employee;"

.


psql -h localhost -U postgres -p 5432 sqlex < sqlexbackup.sql (postgres=# CREATE DATABASE sqlex;)
psql -h localhost -U postgres -p 5432 northwind < northwindexbackup.sql (postgres=# CREATE DATABASE northwind; - northwind_user)
psql -h localhost -U postgres -p 5432 employee < empexbackup.sql (postgres=# CREATE DATABASE employee;)

D:\PostgreSQL\sqlex>psql -h localhost -U postgres -p 5432 sqlex < sqlexbackup.sql

psql -h localhost -U postgres -p 5432 my_new_database < my_old_database.backup
psql "dbname=sqlex host=localhost user=postgres password=abc port=5432"

Click


list user : \du

\l+ to list databases
\d+ to list all tables in current search_path schema in current database.
psql -U pgadmin -l


To switch databases:
\connect database_name or \c database_name


Create User : 
psql -U postgres -c "CREATE USER my_app CREATEDB PASSWORD 'my_password'"

psql -U postgres -c "CREATE USER user3 CREATEDB PASSWORD 'abc'"
psql -U postgres -c "CREATE USER northwind_user CREATEDB PASSWORD 'abc'"
psql -U postgres -c "CREATE USER emp CREATEDB PASSWORD 'abc'"

Example


D:\PostgreSQL\sqlex>psql -U postgres
Password for user postgres:
psql (11.0)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=# create database sqlex;
CREATE DATABASE
postgres=#

https://www.w3resource.com/PostgreSQL/postgresql-backup-restore.php
https://itnext.io/ngrx-best-practices-for-enterprise-angular-applications-6f00bcdf36d7

Leave a Reply

Your email address will not be published. Required fields are marked *