-
Become the postgres user
$ sudo su - postgres
-
Open the PostgreSQL interactive terminal
$ psql
-
Get help with psql commands
\?
-
List the users
\dg
-
List all the databases
\l
Create a superuser with your login name (app-createuser.html)
sudo -u postgres createuser --pwprompt --superuser joe
Creating new users with the create user client program
-
Run the create user program
createuser --pwprompt --superuser --echo joe Enter password for new role: xyzzy Enter it again: xyzzy CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
Creating new users with CREATE USER from PostgreSQL interactive terminal
-
Become the "postgres" user
sudo su - postgres
-
Open the PostgreSQL interactive terminal
psql
-
Run the CREATE USER SQL command
CREATE ROLE jonathan LOGIN;
-
Become the "postgres" user
$ sudo su - postgres
-
Open the PostgreSQL interactive terminal
$ psql
-
DROP USER IF EXISTS joe
How to update users http://www.postgresql.org/docs/current/static/sql-alteruser.html
ALTER USER david WITH PASSWORD 'hu8jmn3';
ALTER USER weight WITH PASSWORD 'weight';
Create a new database with createdb
-
Run creatdb
createdb demo --owner railsapp
Create a new database with createdb
-
Become the postgres user
sudo su - postgres
-
Run the "CREATE DATABASE" SQL command
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace; CREATE DATABASE bootstrap_development OWNER bootstrap; CREATE DATABASE bootstrap_test OWNER bootstrap; CREATE DATABASE bootstrap_production OWNER bootstrap;
service --status-all
sudo service postgresql-8.4 --full-restart
In order to connect to template0, we need to change that flag:
template1=# UPDATE pg_database SET datallowconn = TRUE
template1-# WHERE datname = 'template0';
UPDATE 1
Now we can connect, and drop the Template1 database in order to replace it with a copy of Template0.
template0=# UPDATE pg_database SET datistemplate = FALSE
template0-# WHERE datname = 'template1';
UPDATE 1
# Risky!!! Backup First!!!
template0=# drop database template1;
DROP
template0=# create database template1 with template = template0;
create database template1 with template = template0 encoding = 'UTF8';
CREATE