Cannot visualise the Postgresql database - sql

I'm new to SQL and I'm following a tutorial on how to create a database. The new database I'm creating is called sample_db.
postgres=# create database sample_db
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
sample_db | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Everything looks great so far. I'm using Datagrip to visualise the database. However when I test the connection I get an error. (See image below):
The error message says "the server requested password-based authentication but no password was provided". However I didn't make a password and the tutorial I followed didn't either.

I believe you created the database using the Postgres local user access (trust authentication). To connect using a JDBC driver (which is the case of DataGrip), you have to create an user and grant him access to the database. This is an usual procedure using psql client:
> sudo -u postgres psql;
> CREATE USER <username> WITH PASSWORD '<password>';
> GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <username>;
After this sequence of commands, you should be allowed to connect to your database using DataGrip

Related

PostgresSQL \d command tells about not existing table

When I type \l into psql I got
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
postgres | postgres | UTF8 | English_United States.1251 | English_United States.1251 |
template0 | postgres | UTF8 | English_United States.1251 | English_United States.1251 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1251 | English_United States.1251 | =c/postgres +
| | | | | postgres=CTc/postgres
So here I have 1 database names postgres, but if I type \d I got
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | db1 | table | postgres
(1 row)
In pgAdmin I can see 1 database named "postgres", so why \d tells me about db1 database? (I created it earlier and dropped)
From the psql help:
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
And as your output shows, db1 is a table, not a database...
DROP TABLE db1; will get rid of it.

Connect local postgres database to Google Cloud

I'm pretty much new to cloud services (and tbh, backend is not my area of expertise) and I'm trying to deploy my local database to google cloud so I can show my app in a portfolio.
The google cloud shell required me to log with my postgres user and password, and I think I got it right because it showed the following in the console, but there's no clue about my local databases
You are now connected to database "postgres" as user "postgres".
postgres-> [\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+-------------------+----------+------------+------------+-----------------------------------------
cloudsqladmin | cloudsqladmin | UTF8 | en_US.UTF8 | en_US.UTF8 |
postgres | cloudsqlsuperuser | UTF8 | en_US.UTF8 | en_US.UTF8 |
template0 | cloudsqladmin | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/cloudsqladmin +
| | | | | cloudsqladmin=CTc/cloudsqladmin
template1 | cloudsqlsuperuser | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/cloudsqlsuperuser +
| | | | | cloudsqlsuperuser=CTc/cloudsqlsuperuser
(4 rows)
postgres->
Also, I'm open to other options if anyone has a better/easier choice to upload a db (unless said option is heroku, because I couldn't make it work)
You have to migrate your on-premises PostgreSQL to GCP.
This documentation will help
https://cloud.google.com/solutions/migrating-postgresql-to-gcp

psql PGP_SYM_DECRYPT : HINT: No function matches the given name and argument types

Since this morning :
psql PGP_SYM_DECRYPT : HINT: No function matches the given name and argument types.
LINE 1: select login,PGP_SYM_DECRYPT(password,'*******') from pa...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Extenstion pg_crypto is present.
so I can't select any data from previously pgp_sym_encrypt queries...
what goes wrong ? how to solve that ?
If the extension was not updated and it was working yesterday, the issue is likely with the search_path. Make sure the path of the extension is set in the search_path.
So, to check where the extension is installed, type \dx and note the schema. Then, type show search_path; and make sure the extension's schema is listed there (it could be public). If not, add the extension's schema to the search path.
To follow investigations I build a copy of the table with less columns.
And tried out the same password visual check :
perso=# select quoi,login,pgp_sym_decrypt(password::bytea,'someKEY') from tempo where quoi ilike '%somesite%' ;
quoi | login | pgp_sym_decrypt
--------------+-----------------------+-----------------
somesite.com | somename#somewhere.fr | foobar
(1 row)
perso=# select quoi,login,pgp_sym_decrypt(password,'someKEY') from tempo where quoi ilike '%somesite%' ;
quoi | login | pgp_sym_decrypt
--------------+-----------------------+-----------------
somesite.com | somename#somewhere.fr | foobar
(1 row)
perso=# \d+ tempo
Table "public.tempo"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
----------+---------+-----------+----------+---------+----------+--------------+-------------
ref | integer | | | | plain | |
quoi | text | | | | extended | |
login | text | | | | extended | |
password | bytea | | | | extended | |
there is no more problems here so there is an issue on the table or on the data store-mode.
perso=# \d+ passwd
Table "public.passwd"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
----------+---------+-----------+----------+-------------------------------------+----------+--------------+-------------
ref | integer | | not null | nextval('passwd_ref_seq'::regclass) | plain | |
quoi | text | | not null | | extended | |
login | text | | not null | | extended | |
password | text | | not null | | extended | |
Indexes:
"passwd_pkey" PRIMARY KEY, btree (ref)
"passwd_password_key" UNIQUE CONSTRAINT, btree (password)
perso=#
perso=# select quoi,login,pgp_sym_decrypt(password,'someKEY') from passwd where quoi ilike '%somesite%' ;
ERROR: function pgp_sym_decrypt(text, unknown) does not exist
LINE 1: select quoi,login,pgp_sym_decrypt(password,'someKEY') fr...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
perso=#
here we have back error & detect text on password column.
So test :
alter table => BIG FAIL it reencoded a second time all datas..
drop test table passwd
restore test table passwd
copy in tempo table the datas
delete datas in passwd table
alter the passwd table
copy data back in passwd table
as a procedure for my test.
so I did :
perso=#
perso=# delete from passwd ;
DELETE 106
perso=# alter table passwd alter column password type bytea using PGP_SYM_ENCRYPT(password::text,'someKEY');
ALTER TABLE
perso=# \d+ passwd
Table "public.passwd"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
----------+---------+-----------+----------+-------------------------------------+----------+--------------+-------------
ref | integer | | not null | nextval('passwd_ref_seq'::regclass) | plain | |
quoi | text | | not null | | extended | |
login | text | | not null | | extended | |
password | bytea | | not null | | extended | |
Indexes:
"passwd_pkey" PRIMARY KEY, btree (ref)
"passwd_password_key" UNIQUE CONSTRAINT, btree (password)
perso=# insert into passwd (ref,quoi,login,password) select ref,quoi,login,password::bytea from tempo ;
INSERT 0 106
perso=#
perso=#
perso=# select quoi,login,pgp_sym_decrypt(password,'someKEY') from passwd where quoi ilike '%somesite%' ;
quoi | login | pgp_sym_decrypt
--------------+-----------------------+-----------------
somesite.com | somename#somewhere.fr | foobar
(1 row)
perso=#
Then I backup the database; and applied similar procedure to fix the issue with success. It might be a better way to do that but this way I understand the process.
both solution in there :
using queries with column:bytea syntax
fix the column type to be:bytea

PostgreSQL: restoring table from sql using psql return 'ERROR: invalid input syntax for integer'

I trying to restore sql dump that looks like this:
COPY table_name (id, oauth_id, foo, bar) FROM stdin;
1 142 \N xxxxxxx
2 142 \N yyyyyyy
<dozen similar lines>
last line in this dump: \.
command to restore:
psql < table.sql
or
psql --file=dump.sql
\d+ table_name:
Table "public.table_name"
Column | Type | Modifiers | Storage | Stats target | Description
---------------------+-----------------------+-------------------------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('connected_table_name_id_seq'::regclass) | plain | |
oauth_id | integer | not null | plain | |
foo | character varying | | extended | |
bar | character varying | | extended | |
Looks sadly that standard method for backup and rollback does not work :(
Version of the psql: 9.5.4, version of the server: 9.5.2

mariaDB - error granting rights to users

I have the following users in my database:
MariaDB [racktables]> select user from mysql.user;
+----------+
| user |
+----------+
| admin |
| rackuser |
| repluser |
| root |
| root |
| root |
| |
| admin |
| rackuser |
| root |
| |
| root |
+----------+
12 rows in set (0.00 sec)
I'm trying to set up permissions but I keep getting the following error message:
MariaDB [racktables]> grant all on racktables.* to rackuser;
ERROR 1133 (42000): Can't find any matching row in the user table
MariaDB [racktables]>
The user clearly exists... I'm not sure why I'm getting this message. Unless.. this is my first crack at using mariaDB. I've imported a mysql database into mariaDB. And I'm assuming that mariaDB's users should be in the mysql.users table.
But maybe I'm wrong? I'm currently reading the mariaDB docs.. but I haven't found my answer yet.
Any tips would be appreciated.
Thanks.
I had to run the
FLUSH PRIVILEGES;
command first... and then the grants worked.