POSTGRESQL STARTER
to add the repo and update the repositories in Ubuntu run:
echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' >> /etc/apt/sources.list.d/pgdg.list wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update apt-get install postgresql-9.4
to add the repo and update the repositories in Ubuntu run:
apt-get install postgresql-9.4 su - postgres /usr/lib/postgresql/9.4/bin/pg_ctl -V pg_ctl (PostgreSQL) 9.4.8 sh /etc/init.d/postgresql status
Check the following sources:
http://www.davidpashley.com/articles/postgresql-user-administration/
http://www.davidpashley.com/articles/postgresql-user-administration/
# main configuration file , check with ps -ef /etc/postgresql/9.4/main/postgresql.conf # The host-based authentication is controlled by the pg_hba.conf file and defines which users can connect to which database and how they can connect to i /etc/postgresql/9.4/main/pg_hba.conf
You can start , stop or check the status of the database server by issuing the following commands.
# your mileage might vary sh /etc/init.d/postgresql status sh /etc/init.d/postgresql stop sh /etc/init.d/postgresql start
You can access the postgre client from the postgre Linux user
su - postgres psql
Connect to database. List tables. List views
-- list all the tables in the connected db postgres=# \dt -- list all the views in the connected db postgres=# \dl
#Run a SQL script (command line) psql -f script.sql databaseName
Provisioning
How-to create a role in postresql
CREATE ROLE role_name WITH LOGIN ENCRYPTED PASSWORD 'pwd' CREATEDB;
How-to create a role in postresql
CREATE ROLE role_name_to_create WITH LOGIN ENCRYPTED PASSWORD 'pwd' CREATEDB CREATEROLE REPLICATION SUPERUSER;
How-to create a role in postresql
ALTER ROLE role_name_to_change CREATEROLE CREATEDB REPLICATION SUPERUSER;
How-to drop a role in postgresql
DROP ROLE role_name_to_drop
Official docs @:
http://www.postgresql.org/docs/9.4/static/sql-createuser.html
http://www.postgresql.org/docs/9.4/static/sql-createuser.html
CREATE USER user_name WITH PASSWORD 'pwd'; -- and verify by listing all the users select * from pg_shadow;
how-to change user password
ALTER USER colin WITH PASSWORD 'letmein';
Just like creating users, there are two ways to remove users, using the command line or SQL. At the command line we would use:
DROP USER user_to_drop ;
Groups are entirely optional in postgresql.
We can see group membership by viewing the pg_group system table. In this example I’ve added alice back into the sales group.
select * from pg_group ;
Groups are only used to simplify granting and revoking privileges for the db admin, and users do not need to be a member of any group.
CREATE GROUP group_to_add WITH USER existing_user ;
You can add or remove users from groups after they have been created using the ALTER GROUP command:
ALTER GROUP groupname [ADD|DROP] USER username [, ... ]
# Backup a database (command line) pg_dump dbName > dbName.sql # Backup all databases (command line) pg_dumpall > pgbackup.sql
Grant all priveledges to a role on a db
GRANT ALL PRIVILEGES ON DATABASE db_name TO role_to_be_granted ;
psql- c "SELECT current_setting('datestyle');";
psql- c "SELECT current_setting('datestyle');";
How-to create database
-- CREATE DATABASE DROP DATABASE IF EXISTS dbName ; CREATE DATABASE dbName; CREATE DATABASE utf8db WITH OWNER owner_role ENCODING 'UTF8';
how-to create table
Check the datatypes docs:
http://www.postgresql.org/docs/9.4/static/datatype.html
Check the datatypes docs:
http://www.postgresql.org/docs/9.4/static/datatype.html
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); -- Add a primary key ALTER TABLE TableName ADD PRIMARY KEY (id);
how-to create an index
-- Create an INDEX CREATE UNIQUE INDEX indexName ON TableName (columnNames); -- Create a Function CREATE OR REPLACE FUNCTION month (timestamp) RETURNS integer AS 'SELECT date_part(''month'', $1)::integer;' LANGUAGE 'sql';
how-to create a function
-- Create a Function CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase boolean DEFAULT false) RETURNS text AS $$ SELECT CASE WHEN $3 THEN UPPER($1 || ' ' || $2) ELSE LOWER($1 || ' ' || $2) END; $$ LANGUAGE SQL IMMUTABLE STRICT; -- and call it SELECT concat_lower_or_upper('Hello', 'World', true);
Pagination , limit
-- The first N records SELECT columns FROM table LIMIT 10; -- Pagination SELECT cols FROM table LIMIT 10 OFFSET 30;
Regular expressions
-- Search using a regular expression SELECT column FROM table WHERE column ~ 'foo.*';
Prepared statements
-- Prepared Statements PREPARE preparedInsert (int, varchar) AS INSERT INTO TableName (intColumn, charColumn) VALUES ($1, $2); EXECUTE preparedInsert (1,'a'); EXECUTE preparedInsert (2,'b'); DEALLOCATE preparedInsert;
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!