So I am using PostgreSQL 12 and pgAdmin4. I created two new schema from pgAdmin4, ml and web in a database testingDB. But when I access them from terminal, I could only see public schema. How can I view other schemas?
$ psql -U awspostgres -h address -p 5432 -d testingDB
If you want the tables in a schema to be visible with \dt, either be explicit:
\dt ml.*
or add the schema to your search_path:
SET search_path = ml, web, public;
If you want the latter change to be persistent for all connections to the database
ALTER DATABASE "testingDB" SET search_path = ml, web, public;
Postgres can have multiple databases under one DBMS instance. Make sure you are connnecting to a database by specifying the name with the -d flag to psql:
$ psql -U awspostgres -h address -p 5432 -d <db name> testingDB
Once connected to postgres (and even if you didn't provide a database name when you started psql), you can show databases with \l and connect to one using \c <db name>
Related
I am trying to migrate bunch of databases from AWS RDS Postresql server to GCP Cloud SQL.
Since both are postgresql engine, I thought it will be a simple solution to take pgdump from aws and do import in gcp.
However I am surprised when import cloud sql failed with error complaining some roles are missing.
Below are the steps which are tried
Dump of database in AWS RDS
pg_dump -h <connection_endpoint> -U root -f db_dump.sql <db_name>
Then I tried to import it in GCP Cloud sql with below command
instance-1:~$ PGPASSWORD=<passwprd> psql --host=<host_name> --port=5432 --username=postgres --dbname=<db_name> < db_dump.sql
SET
SET
SET
SET
SET
set_config
------------
(1 row)
SET
SET
SET
CREATE SCHEMA
ERROR: must be member of role "rdsadmin"
CREATE SCHEMA
ERROR: must be member of role "root"
CREATE SCHEMA
ERROR: must be member of role "root"
CREATE SCHEMA
ERROR: must be member of role "root"
CREATE SCHEMA
ERROR: must be member of role "root"
CREATE EXTENSION
ERROR: must be owner of extension plpgsql
CREATE EXTENSION
COMMENT
SET
SET
CREATE TABLE...
As you can see rdsadmin and root roles are missing.
How to make sure that these missing roles are present in GCP Cloud sql with correct settings because even after creating roles with same name in cloud sql it doesn't succeed?
Any solution please?
Answering my own question as I found a solution for it.
Since rdsAdmin user is created by AWS for administrative tasks on RDS cluster. Taking pg_dump without owners and restoring it without owners does the trick and I am able to perform restore in cloud sql.
pg_dump -Fc -O -h <rds-host> -U <user> -d <db> > db.dump
pg_restore -U postgres -d <db> -h <cloudsql-host> -v --no-owner db.dump
pg_restore needs formatted compressed output to restore. To fulfil that -Fc is used in pg_dump command
First of all, I am new to PostgreSQL.
So am I right thinking that one cannot run most of the psql util commands nor non-db-specified sql commands if there isn't a db with same name of the current user's?
That is saying e.g., if I run psql "show databases;" as user postgres while there isn't a db called "postgres", I won't be able to run the command.
Question is that in this case, one cannot find the list of the dbs before knowing any of db exits, is that how it works?
You have to connect to a database. By default, the databases "template1" and "postgres" will exist and will accept connections.
If your PostgreSQL admin has changed things in such a way that you can't connect to either of those databases, you'll have to do one of two things.
Ask the PostgreSQL admin what database you're supposed to connect to.
Create a database, then connect to it. There's more than one way to do this.
If you have CREATEDB privileges, you can create a database on the psql command line. For example, I have CREATEDB privileges here, so I can do this, which creates the database "mike" and exits.
$ psql -h localhost -p 5435 -U mike -c "create database mike"
Now I can connect to "mike" by either taking advantage of the default database name, or by specifying it.
$ psql -h localhost -p 5435 -U mike
$ psql -h localhost -p 5435 -U mike mike
You can. If you connect (with proper user, usually postgres) to the postgres database there are several tables on the pg_catalog (PostgreSQL) among those is pg_database table a simple select * from pg_database will show all databases.
Here is an image showing that on pgAdmin III Tool
There is no way of doing exactly what you want without, at least, knowing the database catalog. The postgres database is default and will exist in all installed instances (unless someone had droped it). All RDBMs is the same they all have the catalog (also named information_schema, or other names depending on vendor) which holds all information about the databases, tables, constraints, etc.
I search for a while to find this answer but with no luck.
The situation:
I have Postgresql currently running on my production environment. I am preparing to scale my database and move it to a large server instance. I made the mistake of setting up the initial database with the postgres user who has all permissions, and I would like the new database to be controlled by a custom user I have created. ie The current database's owner is postgres, and I want the new database owner to be pooper.
To dump, I am running:
pg_dump -d database_name > database_name.sql
To restore on separate machine, I am running:
psql database_name < database_name.sql
If the user is the same, ie both postgres, then it will work just fine, but when switching users, my app does not load correctly. Is there a secret to the madness. Nothing stood out to me.
My system:
Debian Wheezy
Postgresql 9.1
Postgis Extension
pg_dump with the --no-owner flag (see pg_dump --help)
Create the new db with the new owner CREATE DATABASE foo OWNER pooper;,
Load via psql -U pooper -d database_name -f database_name.sql.
I know two methods of copying a postgres database, but both of them require you to have exclusive access to the database, something you do not have while trying to copy a database from production in order to use it for testing something, like a software upgrade/migration.
psql>create database mydb_test with template mydb owner dbuser;
ERROR: source database "mydb" is being accessed by other users
>createdb -O dbuser -T mydb mydb_test
createdb: database creation failed: ERROR: source database "mydb" is being accessed by other users
That worked:
psql
create database mydb_test owner dbuser;
\q
pg_dump mydb|psql -d mydb_test
I moved my PostgresQL database from one hard drive to another using
pg_dump -U postgres db_name > db_name.dump
and then
psql -U postgres db_name < db_name.dump
I created the database db_name the same way in both instances. In the new database when I run my Java program with a JPA query (or a JDBC query) I get this error:
"ERROR: relation "table1" does not exist"
The query is:
select count(0) from table1
I know I've got a connection because if I change the password in the connection parameters I get an error.
For some reason in the new PostgresQL instance it thinks that table1 does not exist in the imported schema.
If I change the query to
select count(0) from myschema.table1
Then it complains about permissions:
"ERROR: permission denied for schema myschema"
Why would the permissions be different?
The table table1 exists in myschema because I can see it in the pgAdmin tool. All the rows were imported into the new PostgresQL instance.
When I do a query from Java the combination of pg_dump and psql created a problem.
What do I need to do to solve this issue?
Are you moving to the same version of PostgreSQL? There might be issues if you make a dump with pg_dump 8.3 and try to restore it in Postgresql 8.4. Anyway, assuming that it is the same version try the following:
Dump all global objects, such as users and groups (don't know if they were missing in your dump):
pg_dumpall -g -U postgres > globals.sql
Dump schema of database:
pg_dump -Fp -s -v -f db-schema.sql -U postgres dbname
Dump contents of database:
pg_dump -Fc -v -f full.dump -U postgres dbname
Now restore.
psql -f globals.sql
psql -f db-schema.sql dbname
pg_restore -a -d dbname -Fc full.dump
That is my $0.02. Hope it helps.
I encountered this problem. Then I realized that I forgot to install postgis extension.
Don't forget to install the extensions you use.
I was able to solve it by changing the database privileges to public CONNECT and the schema privileges for public and postgres = USAGE and CREATE.
My backup scripts apparently didn't preserve the privileges, at least not when moving from 8.3 to 8.4.