Postgres: Difference between revisions
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Useful commands= | =Useful commands= | ||
/* Connect */ | |||
<syntaxhighlight lang="bash"> | |||
psql -d mydb -U myuser | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="psql"> | <syntaxhighlight lang="psql"> | ||
/* Show users */ | /* Show users */ | ||
Line 52: | Line 58: | ||
/* Create Index */ | /* Create Index */ | ||
CREATE UNIQUE INDEX name_idx ON employees (name) | CREATE UNIQUE INDEX name_idx ON employees (name) | ||
/* Command History */ | /* Command History */ | ||
Line 60: | Line 66: | ||
<syntaxhighlight lang="psql"> | <syntaxhighlight lang="psql"> | ||
create user user_name with encrypted password 'mypassword' | create user user_name with encrypted password 'mypassword' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Create Super User= | =Create Super User= |
Revision as of 21:14, 22 September 2022
Introduction
This is a page for keeping track of using postgres
Install
Nice and easy lemon squeezy
sudo apt install postgresql postgresql-contrib
Getting to CLI
Login and envoke the CLI
sudo -i -u postgres
psql
Quiting..
\q
Useful commands
/* Connect */
psql -d mydb -U myuser
/* Show users */
\du
/* Show databases */
\l
/* Show tables */
\dt
/* Set password */
alter user test with encrypted password 'testpass'
/* Create database */
CREATE DATABASE testdb
/* Grant Privileges */
grant all privileges on database testdb to test
/* Use database */
\c testdb
/* Create Table */
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
"Name" VARCHAR (50),
birthdate DATE CHECK (birthdate > '1900-01-01'),
salary numeric CHECK(salary > 0)
);
/* Describe the Table */
\d test_table
/* Create Index */
CREATE UNIQUE INDEX name_idx ON employees (name)
/* Command History */
\s
create user user_name with encrypted password 'mypassword'