It looks like you might need a couple of randomly selected words from a dictionary. This is hard to say, given the lack of clarity.
ACCIDENTAL DICTIONARIES
The best way to pick random dictionary words is probably to end PHP using a password generator that does this for you.
You can do this in PostgreSQL using a dictionary table with one word for each row, though:
SELECT word FROM dictionary ORDER BY random() LIMIT 2;
Performance will be really terrible with a large dictionary. This can be done much faster if the dictionary does not change and there is a unique word_id with no spaces in the numbering, allowing you to write:
CREATE OR REPLACE FUNCTION get_random_word() RETURNS text AS $$ SELECT word FROM dictionary WHERE word_id = ( SELECT width_bucket(random(), 0, 1, (SELECT max(word_id) FROM dictionary)) ); $$ LANGUAGE sql; SELECT get_random_word() || ' ' || get_random_word();
for a table, for example:
CREATE TABLE dictionary(word_id serial primary key, word text UNIQUE NOT NULL);
This will result in consistent results if there are no spaces in the word numbering and if word_id is unique or PRIMARY KEY . He can produce the same word twice. If you want to avoid this, you will need a recursive CTE or some kind of PL / PgSQL.
RANDOM GIBBERISH
If you really want really random strings that are already well described here in Stack Overflow. See How to create a random string suitable for a session id in PostgreSQL? among the others; look at this search .
To ensure uniqueness, just add a UNIQUE . Ask your app test to check if unique_violation was raised when you INSERT edited a row and inserted it with a new random identifier if a violation occurred. If you like, you can automate this using the PL / PgSQL helper routine, although it will still be subject to race between concurrent inserts in different transactions.
Craig Ringer Dec 03 2018-12-12T00: 00Z
source share