How can I set password in SHA format? - postgresql

I want set a nationalnumber as password in SHA format with subquery
UPDATE ct_user SET password = crypt(select nationalnumber from ct_user where username='6960004', gen_salt('md5'));

Related

How can I return a column value in postgresql when inserting into the table?

I have a table of in the format
userid uuid DEFAULT uuid_generate_v4 (),
username VARCHAR NOT NULL,
email VARCHAR NOT NULL,
password VARCHAR NOT NULL,
password_salt VARCHAR NOT NULL,
creation_date timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
When I insert into the table, I a uuid is generated. How can I retrieve the uuid generated from an insert command without sending another select query? The best I have is something like this:
INSERT into user (username, email, password, salt) VALUES ('user', 'email', 'password', 'salt')
RETURNING uuid;
but it doesn't work because it doesn't know what the uuid variable I am referring to.
You return the column name not a data type:
insert into user (username, email, password, salt)
values ('user', 'email', 'password', 'salt')
returning userid;

How to crypt mine password in postgreSQL?

Trying to crypt mine function and get it back,need your help tell me please what i am doing wrong ?
SELECT user_id
FROM users
WHERE email = 'Natali#gmail.com' AND u_password = crypt(u_password, '#kjvfhjh88976');
// Null result
INSERT INTO users (user_id, nick_name, email, u_password)
VALUES
(87678655, 'Natali1990#', 'Natali#gmail.com', crypt('#kjvfhjh88976', gen_salt('bf')));
SELECT user_id FROM users WHERE email = 'Natali#gmail.com'; // Working
You are using the pgcrypto package incorrectly, and given that it is confusing, this is not a surprise. Your current insert seems fine:
INSERT INTO users (user_id, nick_name, email, u_password)
VALUES
(87678655, 'Natali1990#', 'Natali#gmail.com',
CRYPT('#kjvfhjh88976', GEN_SALT('bf')));
Then, to authenticate a user, use a SELECT looking something like the following:
SELECT u_password = CRYPT('#kjvfhjh88976', u_password)
FROM users
WHERE email = 'Natali#gmail.com';
This would return true if the user entered the correct password. You may read more about this in the Postgres documentation;

Generate related fields in Red Gate SQL Data Generator 3?

I'm using Red Gate SQL Data Generator 3. What I'm wanting to do is to generate test data where there are related fields in each row. For example, I want to generate a row of data that looks like this:
Username: CONTOSO\FFlintstone
FullName: Flintstone, Fred
Email: FFlintstone#contoso.com
Programatically, I'd want something like (pseudo-code):
Generate _lastname, _firstname
_username = first-letter of _firstname + _lastname
Fullname = _lastname + ", " + _firstname
Username = "CONTOSO\" + _username
Email = _username + "#contoso.com"
All the data generator samples I saw were for a single field, and didn't allow or consider needing to populate a row with related fields. I did not see a means of doing this within the product directly. Also, at the moment, the user forums at Red-Gate are down, so no help there.
Is this possible to do within the product? If so, could somebody post an example?
As a proof of concept, I created a dummy table with a primary key and some fields - firstname, surname, username and email.
firstname was just set to the generic "First Name" generator, as the surname was set to the "Last Name" generator.
I then used the "Simple Expression" generator for the username and email fields. The expression for the username generator was 'CONTOSO\\'+firstname[:1]+surname. For the email column, the expression I used was firstname[:1].lower()+surname.lower()+'#contoso.com'
This image shows the resulting data I managed to generate
I hope this helps.

How can I use an hstore column type with Npgsql?

I have a table with the following schema:
CREATE TABLE account
(
id serial primary key,
login varchar(40) not null,
password varchar(40) not null,
data hstore
);
I'd like to use an NpgsqlCommand object with parameters to retrieve and store the account data from my application. Which DbType do I have to use for the NpgsqlParameter? The enum NpgsqlDbType does not have a value for hstore. Can I use a Dictionary or HashTable as value of the NpgsqlParameter object?
When I use a JSON column I can create a parameter of type NpgsqlDbType.Text, use a library like JSON.Net to serialize an object to a JSON string and send an SQL statement like that:
INSERT INTO account (login, password, data) VALUES (:login, :password, :data::json)
Unfortunately this does not work with an hstore column. I get a syntax error when I try to do this:
INSERT INTO account (login, password, data) VALUES (:login, :password, :data::hstore)
The string I pass to the data parameter looks like this:
'key1 => "value1", key2 => "value2"'
Thank you, Francisco! I saw in the log that the single quotes (') at the beginning and the end of the string are escaped when they are passed to PostgreSQL. When I pass
key1 => "value1", key2 => "value2"
instead, I can insert the data into the hstore column.

How do i update a table column based on a condition?

Two table:
StoreInfo:
UserId uniqueidentifier
StoreNo nvarchar
UserName nvarchar
Password nvarchar
UserInfo:
UserId uniqueidentifier
UserName nvarchar
Password nvarchar
the UserId on StoreInfo is currently null. How do i update StoreInfo's UserId with UserInfo's UserId based on StoreInfo's UserName and Password is match to the UserName and Password from UserInfo.
the following is the query that i wrote which update the entire UserId in StoreInfo with the first UserId from UserInfo so i know it's wrong.
declare #UserName nvarchar(255)
declare #Password nvarchar(25)
declare #UserId uniqueidentifier
select #UserName = UserName, #Password = Password, #UserId = UserId
from UserInfo
select UserId, Password
from FranchiseInfo
where UserID = #UserName and Password = #Password
update FranchiseInfo
set UserI = #UserId
The update would look like this
update storeinfo
set userid = u.userid
from userinfo u
inner join storeinfo s on (s.username = u.username and s.password = u.password)
where userid is null
The most efficient way is the UPDATE ... FROM syntax, e.g.
UPDATE StoreInfo
SET
UserId = ui.UserId
FROM
StoreInfo si
INNER JOIN UserInfo ui ON ui.UserName = si.UserName AND ui.Password = si.Password;
UPDATE StoreInfo
set UserId = ui.UserId
from StoreInfo si
inner join UserInfo ui
on ui.UserName = si.UserName
and ui.Password = si.Password
where si.UserId is null
This will update all rows in the table where UserId is not set. Build out the where clause if you only want to update selected rows. (I haven't tested this, so watch for typos!)