Connect local postgres database to Google Cloud - sql

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

Related

How to create Header over Header

Table:
id | BL | ML |BL | ML
---------------------------------------------------------
1 | Field01 |Name | Field34 | Field36
2 | Field02 |Age | Field35 | Field37
Required result:
Id | Open | Closed
---------------------------------------------------------
| BL | ML |BL | ML
---------------------------------------------------------
1 | Field01 |Name | Field34 | Field36
2 | Field02 |Age | Field35 | Field37
This is not possible in just SQL Server. You should be handling this type of display within your presentation tool.
It seems weird to do this kind of thing in SQL Server. I don't think you can even do this in SQL Server. If you have access to Python, you can use Python to do this kind of thing, which is called a MultiIndex. This may be going off on a tangent, but you can refer to the link below as to how to create a MultiIndex.
https://jakevdp.github.io/PythonDataScienceHandbook/03.05-hierarchical-indexing.html

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.

Cannot visualise the Postgresql database

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

Building a Database Model for Role Based Access Control

I'm trying to make a role based access control system, but the problem comes when I approach the Database Part of It.
Should I make two models, Role and Permission, and then make a many to many relationship between role and permission or what?
My User Model looks something like this:
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+--------------------
id | uuid | | not null | uuid_generate_v4()
name | character varying(50) | | not null |
email | character varying(320) | | not null |
avatar | text | | |
password | text | | not null |
phone | character varying(30) | | |
created_at | timestamp without time zone | | not null | now()
updated_at | timestamp without time zone | | | now()
companyId | uuid | | |
roleId | uuid | | |
So I just have 1 to many relation ship between user and role.
Mainly it's done with a single role and multiple users associated with it.
Roles can be assigned privileges and users can be assigned roles.
You can refer following for the same.
Role-based access control in Postgres/mongo

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.