2

I am not getting a clue to:

  1. simply login to postgreSQL
  2. Create a database
  3. Add a table
  4. Insert a record
  5. Delete , update etc

These things are normally very very easy using mysql . Can someone help me setup following alternative for postgresql

a) Reset default password -- Very Clean description , I do not find same level of clarity for PostgreSQL (Any documentation link is highly appreciated)

b) We know the superuser for mysql is "root" what is the same for PostgreSQL

c) from command line how to ( PostgreSQL ones ?):

 mysql -uroot -proot  
   create database testdb; 
   use testdb;
   create table test(id int(11) auto_increment, name varchar(20), primary key(id));
   insert into test(name) values("testing one"),("testing two"),("testing three"),("testing four");
   select * from test;
   update test set name=concat(name,now()) where id =3;
   delete from test where id =4;
   drop table if exists test;
   drop database if exists testdb;

EDIT MAC OS # Default password reset

sudo mate /Library/PostgreSQL/9.2/data/pg_hba.conf

replaced (md5 with trust)

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

with

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

save

executed the Reload Configuration.app

 login to postgresql without password : 
  $ psql -U postgres
  postgres# ALTER USER postgres WITH PASSWORD 'new password';
  \q  

-revert back all the changes in pg_hba.conf (replace trust with md5) and save

-reload configuration

Now I can login to postgresql with new password

psql -U postgres

Password for user postgres:[my new password]

S-Man
  • 22,521
  • 7
  • 40
  • 63
sakhunzai
  • 13,900
  • 23
  • 98
  • 159

4 Answers4

2

To login:

psql -d postgres_dbname -U postgres

Create Database:

create database  testuser;

\c testuser;  /*use testuse as mysql*/

Create Table:

create table employee (Name char(20));

Insert :

 insert into employee VALUES ('XAS');

Update Link

Delete Link

Reset Password : See Here & See Here Too

thar45
  • 3,518
  • 4
  • 31
  • 48
1

Simply login to postgreSQL

psql -U postgres -W template1
-U = username
postgres is root 
-W = ask for password
tempalte1 = default database

Create a database

-- Create the database from within postgresql
create database my_database_name
-- Connect to the database
\c my_database_name

-- Create the database without logging in
createdb -U postgres -W my_database_name
  1. Add a table
  2. Insert a record
  3. Delete , update etc

All the above from 3 to 5 are like in MySQL

For resetting postgres forgotten password this link is a good reference.

Desislav Kamenov
  • 1,193
  • 6
  • 13
  • 1
    $ psql -U postgres -W template1 Password for user postgres: psql: FATAL: password authentication failed for user "postgres" – sakhunzai Oct 15 '12 at 06:11
  • do you know your postgres password? Do you have a password at all? – Desislav Kamenov Oct 15 '12 at 06:14
  • thanks , for `psql -U postgres -W template1` prompts me `Password for user postgres:` which I dont know and need to set ( I am on MAC OS , I don't know if that affect anything ) – sakhunzai Oct 15 '12 at 06:14
  • That is what I need, I need to set the `ROOT or superuser password ` – sakhunzai Oct 15 '12 at 06:16
  • then follow the link in my post ;) – Desislav Kamenov Oct 15 '12 at 06:16
  • but before that can I know what is the default one , I do not remember setting any password/username during postresSQL installation(MAC OS).I ll try your reset link ,that look reasonable – sakhunzai Oct 15 '12 at 06:28
  • As far as i remember, the password is being generated during installation and written in a file. Also if you make: `sudo su - postgres` and then `psql ` you should be logged without a password – Desislav Kamenov Oct 15 '12 at 06:43
1

postgresql is a completely different system than mysql; so do not assume things will be like mysql. They are completely different animals entirely; some of your SQL statements might not work, especially if you are using a MySQL proprietary command.

  1. To login to postgresql, use the psql command shell
  2. CREATE DATABASE
  3. CREATE TABLE
  4. INSERT
  5. For all other basic SQL commands, consider going through the tutorial

User access control is something more fine grained and detailed in postgresql. There are users and roles. A user is simply a role that has the ability to login (like MySQL), but in postgresql you can have roles (an account) that cannot login.

What access a role has is defined in pg_hba.conf. This file defines if a role can login at all, by what means are they authenticated, from where they can login and what database they have access to.

ALTER USER is used to reset credentials.

The "root user" for postgresql is typically postgres; and this is a system user that is created during the install process. For Windows, the binary installer will ask if you want to launch the service as this user as well.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • can you just help me login to postgreSQL ? Or reset the root password ? thanks for your description – sakhunzai Oct 15 '12 at 06:19
  • 1
    To login `su -` (switch to `root`), then `su postgres`, then type `psql`. – Burhan Khalid Oct 15 '12 at 06:34
  • 1
    `sudo postgres psql` `Password:` "root" execution of the PostgreSQL server is not permitted. The server must be started under an unprivileged user ID to prevent possible system security compromise. See the documentation for more information on how to properly start the server. – sakhunzai Oct 15 '12 at 06:36
  • @Hector @sakunzai - you need to make sure the server is running first, or if you are connecting to a remote server, identify it with `-h` – Burhan Khalid Jan 07 '15 at 13:55
0

Please take a look at this PostgreSQL error 'Could not connect to server: No such file or directory'

Try to install postgresApp, this solved my problem which was the same of yours.

Community
  • 1
  • 1
porthfind
  • 1,581
  • 3
  • 17
  • 30