Issue with Postgres roles after exporting then restoring from Heroku - postgresql

I'm new to Postgres and Heroku. I've inherited a project with a Postgres DB hosted on Heroku and I'd like to export it from there for local use inside Docker. I've got as far as exporting and then restoring (via pg_restore) the DB inside my container, but when it builds my DB I get loads of warnings like:
pg_restore: from TOC entry 285; 1259 16897 TABLE foo u2ae1fgct609sl
pg_restore: error: could not execute query: ERROR: role "u2ae1fgct609sl" does not exist
Command was: ALTER TABLE public.foo OWNER TO u2ae1fgct609sl;
Presumablty this is because I've exported/restored the DB but not the corresponding users. Heroku doesn't seem to mention this part in its docs on exporting/restoring/importing Postgres DBs.
What can I do about this? Can I somehow tell it to set as the owner the DB user that Docker created when spinning up the container, or am I on the wrong lines?

Related

Can't see tables in pgAdmin4 that were created through dockerized PostgreSQL

I'm running PostgreSQL through a Docker container. I entered the container and used psql to connect to postgres. I created two databases then created a table in test_db. I checked pgAdmin4 and the databases were there but the students table is not showing up in the UI. Not sure why if the databases are showing up?
I see you connected to 'test_db' database in pgadmin, but to 'postgres' database in psql. If you want to see 'students' table from 'test_db' database in psql, you need connect to 'test_db' database firstly instead of 'postgres' database. You can use '\c test_db' command for that in psql, or '-d test_db' command-line parameter when launching psql from command line.

PostgreSQL 13: create empty copy of database

I have a AWS RDS PostgreSQL 13 server with some databases. I have to create an empty copy of one database (empty means schema (tables, views, functions) + security (users, roles)).
Is pg_dump -s what I am looking for?
Thanks!
pg_dump -d db_name -s. You will also need to do pg_dumpall -g to get the global data e.g. roles. This will get all global data for the Postgres cluster, so you may have more then you need for the particular database.
Postgres allows the use of any existing database on the server as a template when creating a new database. I'm not sure whether pgAdmin gives you the option on the create database dialog but you should be able to execute the following in a query window if it doesn't:
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
Still, you may get:
ERROR: source database "originaldb" is being accessed by other users
To disconnect all other users from the database, you can use this query:
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'originaldb' AND pid <> pg_backend_pid();

Cannot drop db in postgres [duplicate]

I can't seem to reset my database while using docker compose. I've tried killing the server, killing just the database, and restarting the machine.
Anyone know the best way to clear out the development database?
Here's what I tried:
docker-compose run web rake db:reset
I am getting this error:
PG::ObjectInUse: ERROR: cannot drop the currently open database
: DROP DATABASE IF EXISTS "postgres"
Couldn't drop database 'postgres'
rake aborted!
I'm using the setup exactly as described by the docker-compose quickstart: https://docs.docker.com/compose/rails/
I have a rails container and a postgres container
You are using the wrong database.
The database postgres is normally not used for user data, but for administrative purposes. For example, if you want to drop a database, you have to be connected to a different database in the PostgreSQL database cluster to issue the SQL statement DROP DATABASE. Normally, the database postgres is used for that purpose, and I have no doubt that Docker does exactly that when it tries to drop a database.
If you really want to drop the database postgres, you'd have to connect to some other database in the cluster. The correct solution, however, is to keep your data in a different database. Then the problem should go away by itself.

How to create table on heroku psql?

I am trying to setup a simple web application with postgres. After I have the application running (no database related code yet).
I added a postgres db addon to app with the command heroku addons:create heroku-postgresql:hobby-dev --app <app_name>
Ran heroku psql heroku pg:psql --app.
Attempted to create a table from the prompt: CREATE TABLE example_table ( a INTEGER );
I see the error:
ERROR: no schema has been selected to create in
LINE 1: CREATE TABLE example_table ( a INTEGER );
Skimming the docs, it looks like users have the permission to CREATE. I am not sure what no schema means for postgreSQL.
Note: I also ran the command DROP OWNED BY current_user CASCADE; (successfully) - it was on top of a setup .sql script to clean, create tables and constraints and add sample data. Unsure if that can affect anything here.
https://dba.stackexchange.com/a/106067/30035
also try first to:
set search_path to "$user", public;
and if it does not help - try granting privs as from link above
It was the command DROP OWNED BY current_user CASCADE;, I reset the database and everything is working as expected. Re-ran the DROP OWNED BY current_user CASCADE; and the problem re-appeared.
Answer would be to avoid that command. Use Heroku reset command instead :
heroku pg:reset --app <app_name>

ERROR: unrecognized configuration parameter "tablespace"

I'm trying to move a database in Postgres 8.2 to a new tablespace, but when running ALTER DATABASE data_base_name SET TABLESPACE TO tbspc_name
the following error appears ERROR: unrecognized configuration parameter "tablespace".
It seems that in Postgres 8.2 you have two options:
move tables one by one, see ALTER TABLE ...;
backup the database, create a new one with a tablespace defined, and restore the backup in the new database, see CRETE DATABASE ....