import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Database - Postgres
Installation
Get Started
New Database
Once packages are installed, We can logged with postgres user:
Then the creation of new Database is easily like this:
Prompt psql
It is possible to execute SQL commands directly to interact with the database by running the psql command.
We can now go through this prompt to create a new user to facilitate the administration of our new DB and give all privileges to it:
CREATE USER {DB User} WITH PASSWORD '{Passwd}';
GRANT ALL PRIVILEGES ON DATABASE {DB Name} TO {DB User};
To make sure than the new user is created correctly:
And here is how to change user's password:
Config files
There are several configuration files:
pg_hba.confpg_ident.confpostgresql.conf
For retrieve location of a configuration file, we can display detail of it from psql prompt.
Example for pg_hba.conf:
postgres=# show hba_file ;
hba_file
---------------------------------
/var/lib/pgsql/data/pg_hba.conf
(1 row)
Commands
Globals
List all DB:
Connect to a DB:
List the DB's tables:
Detail of a table:
Display active connection:
Delete a table:
Rename a table:
Rename a table's column:
Change display mode:
Backup and Restore
See the command for backup database:
See the command for restore database:
Examples
DB Backup:
$ su - postgres
$ pg_dump my_db > my_db.dump
$ ls -l my_db.dump
-rw-r--r-- 1 postgres postgres 777927 Jul 15 11:54 my_db.dump
Restore DB (With remove of old version):